1. 程式人生 > >Convert.ChangeType不能處理Nullable型別的解決辦法(轉)

Convert.ChangeType不能處理Nullable型別的解決辦法(轉)

 

https://www.cnblogs.com/patrickyu/p/3211115.html

在做一個ORMapping功能的時候發現,Convert.ChangeType不能處理nullable型別,比如int?.

解決辦法也很簡單,貼出完整的程式碼(大部分程式碼來自網路),注意下面程式碼沒經過完整測試,不要直接用在專案裡:

 public delegate void SetValue<T>(T value);

   

 

public static class ORMapping<T> where T : class, new()
    {
        private static Delegate CreateSetDelegate(T model, string propertyName)
        {
            MethodInfo mi = model.GetType().GetProperty(propertyName).GetSetMethod();
            //這裡構造泛型委託型別
            Type delType = typeof(SetValue<>).MakeGenericType(GetPropertyType(propertyName));
            return Delegate.CreateDelegate(delType, model, mi);
        }
        private static Type GetPropertyType(string propertyName)
        {
            return typeof(T).GetProperty(propertyName).PropertyType;
        }
        
        public static IList<T> ConvertToBusinessEntityList(DataTable dt)
        {
            IList<T> list = new List<T>();
            if (dt == null || dt.Rows.Count < 1) return list;
            Delegate setDelegate;
            foreach (DataRow dr in dt.Rows)
            {
                T model = new T();
                foreach (DataColumn dc in dt.Columns)
                {
                    setDelegate = CreateSetDelegate(model, dc.ColumnName);
                    Type type = GetPropertyType(dc.ColumnName);
                    Type underlyingType = Nullable.GetUnderlyingType(type);
                    if (DBNull.Value != dr[dc.ColumnName])
                    {
                        setDelegate.DynamicInvoke(Convert.ChangeType(dr[dc.ColumnName], underlyingType ?? type));
                    }
                }
                list.Add(model);
            }
            return list;
        }
    }

 

https://www.cnblogs.com/patrickyu/p/3211115.html

在做一個ORMapping功能的時候發現,Convert.ChangeType不能處理nullable型別,比如int?.

解決辦法也很簡單,貼出完整的程式碼(大部分程式碼來自網路),注意下面程式碼沒經過完整測試,不要直接用在專案裡:

 public delegate void SetValue<T>(T value);

   

 

public static class ORMapping<T> where T : class, new()
    {
        private static Delegate CreateSetDelegate(T model, string propertyName)
        {
            MethodInfo mi = model.GetType().GetProperty(propertyName).GetSetMethod();
            //這裡構造泛型委託型別
            Type delType = typeof(SetValue<>).MakeGenericType(GetPropertyType(propertyName));
            return Delegate.CreateDelegate(delType, model, mi);
        }
        private static Type GetPropertyType(string propertyName)
        {
            return typeof(T).GetProperty(propertyName).PropertyType;
        }
        
        public static IList<T> ConvertToBusinessEntityList(DataTable dt)
        {
            IList<T> list = new List<T>();
            if (dt == null || dt.Rows.Count < 1) return list;
            Delegate setDelegate;
            foreach (DataRow dr in dt.Rows)
            {
                T model = new T();
                foreach (DataColumn dc in dt.Columns)
                {
                    setDelegate = CreateSetDelegate(model, dc.ColumnName);
                    Type type = GetPropertyType(dc.ColumnName);
                    Type underlyingType = Nullable.GetUnderlyingType(type);
                    if (DBNull.Value != dr[dc.ColumnName])
                    {
                        setDelegate.DynamicInvoke(Convert.ChangeType(dr[dc.ColumnName], underlyingType ?? type));
                    }
                }
                list.Add(model);
            }
            return list;
        }
    }