.Net Compact中PropertyInfo.SetValue()的ArgumentException

时间:2014-01-08 18:02:49

标签: c# .net compact-framework dapper dapper-lite

我正在使用DapperLite,一个Dapper的端口,用于Compact Framework。我在ArgumentException文件的指定行显示以下方法中的SqlMapper.cs,没有其他信息。

真正挣扎着寻找下一个或者实际上已经破坏的东西。

/// <summary>
    /// Populate a references type from an IDataRecord by matching column names to property names.
    /// </summary>
    private static void PopulateClass(object objectClass, IDataRecord reader)
    {
        Type theType = objectClass.GetType();
        PropertyInfo[] p = theType.GetProperties();


        // Only set properties which match column names in the result.
        foreach (string columnName in GetColumnNames(reader))
        {
            string colName = columnName;
            object value = reader[colName];
            PropertyInfo pi = p.FirstOrDefault(x => x.Name == colName);


            if (pi == null || value == DBNull.Value) continue;


            Type columnType = value.GetType();
            Type actualType = Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType;


            // Check for a directly assignable type
            if (actualType == columnType || actualType == typeof(string) || actualType == typeof(int))
            {
                // Error generated here
                pi.SetValue(objectClass, value, null);
            }
            else
            {
                value = actualType.GetMethod("Parse", new[] { typeof(string) }).Invoke(null, new[] { value });
                pi.SetValue(objectClass, value, null);
            }
        }
    }

此时的值为objectClass = Models.Employeevalue=1。方法中更进一步的colNameIdint模型中为Employee


根据评论添加员工模型:

namespace BioPadPunchClock.Models
{
    public class Employee
    {
        public int Id { get; set; }
        public int CDB_Employee_PK { get; set; }  //Employee PK in the Central DB. If .null., new emp record on this timeclock needs to be pushed to the CDB.
        public string Name { get; set; }
        //public string FaceImage { get; set; }  //path to Jpeg image 136x90 pixels .\NAND\pictures\  named as <pk>.jpg
        public DateTime? BirthDate { get; set; }
        public string Status { get; set; }  //Full Time – Hourly, Inactive, Terminated, Part Time - Hourly  
        public DateTime? LastPunch { get; set; }
        public string LastPunchDirection { get; set; }  //IN or OUT
        public string PIN { get; set; }  // 4 digit
        public int CardNum { get; set; }  //HID Card Number
        public int CardFacCode { get; set; }  //HID Card Facility Code
        public string FpTemplate { get; set; }  //Fingerprint Template (registered fingerprint)
        public DateTime? LastUpdate { get; set; }  //When this employee record was last sync’d to the CDB. This value will match exactly in the LDB and CDB after a successful sync.
        public int DayTotal { get; set; } // Minutes cumulative for the current day at time of last punch
        public int PayPeriodTotal { get; set; } //Minutes cumulative for the current pay period at time of last punch
        public string Department { get; set; }
        public string SSN { get; set; }
        public bool ShouldPromptDept { get; set; } //Prompt employee to choose department each time
    }
}

进一步的发现......

当我查看我的Model.Employee时,所有string属性都说Could not evaluate expression带有红色感叹号。这是线索还是正常?


进一步的发现......

不确定这是否重要但是当我单步执行代码并检查值时,我注意到actualType == Int32columnType==Int64,而value正在评估1 }}。

不确定是否有任何区别,因为有一个actualType.Equals(typeof(int)),但我想提到它。

1 个答案:

答案 0 :(得分:1)

我们用最后一条信息在GitHub上找到了这个。

由于columnType列的Int64Id,因此将模型中Id列的类型更改为Int64将解决此问题。