GetProperty反射赋值SetValue报错:类型“System.Byte”的对象无法转换为类型“System.Nullable`1[System.Int32]”。

在数据库读取DataTable进行实体转换的时候报错,仔细看数据库的类型是tinyint,null,而实体类中的类型是int?,这时候就会报错
原来出错的代码是这样的:

       private static void GetPropertyInfo<T>(DataTable table, Type type, int i, T item) where T : class
        {
            try
            {
                PropertyInfo Property;
                object RowCol;
                foreach (DataColumn column in table.Columns)
                {
                    RowCol = table.Rows[i][column];
                    Property = type.GetProperty(column.ColumnName);
                    if ((RowCol != DBNull.Value) && (Property != null))
                    {
                        Property.SetValue(item, table.Rows[i][column], null);//这里报错
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

参考过一些文章后,发现在反射的时候内部会进行内部限制,像string转int,byte转nullable都是会报错的。解决方法有以下两种:
1、设置为正确的类型。例如数据库是tinyint,null,那么类的类型应为byte?。推荐此种方法,严格核对格式。
2、在设置值的时候,先进行类型转换,但这个要区分一般的类型和nullable类型,见如下代码。不推荐此方法,虽然可以偷懒,但多一层转换非常影响效率。

 public static object ConvertTo(object convertibleValue, Type type)
        {
            if (!type.IsGenericType)
            {
                return Convert.ChangeType(convertibleValue, type);
            }
            else
            {
                Type genericTypeDefinition = type.GetGenericTypeDefinition();
                if (genericTypeDefinition == typeof(Nullable<>))
                {
                    return Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(type));
                }
            }
            throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, type.FullName));
        }

        private static void GetPropertyInfo<T>(DataTable table, Type type, int i, T item) where T : class
        {
            try
            {
                PropertyInfo Property;
                object RowCol;
                foreach (DataColumn column in table.Columns)
                {
                    RowCol = table.Rows[i][column];
                    Property = type.GetProperty(column.ColumnName);
                    if ((RowCol != DBNull.Value) && (Property != null))
                    {
                        Property.SetValue(item, ConvertTo(table.Rows[i][column], Property.PropertyType), null);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

参考:https://www.cnblogs.com/Chavezcn/p/7537211.html
https://q.cnblogs.com/q/3195/
https://blog.csdn.net/xiaohan2826/article/details/8536074

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值