zoukankan      html  css  js  c++  java
  • DataToEnity

      public class DataToEnity<T> where T : new()
        {
            public static T DataRowToEntity(DataRow dr)
            {
                //1.获取类型
                Type type = typeof(T);
                //2.创建实例
                T temp = Activator.CreateInstance<T>();
                //3.获取属性
                PropertyInfo[] properties = type.GetProperties();
                // 4.遍历属性,对属性设置 对应的值
                foreach (PropertyInfo p in properties)
                {
                    //5. 判断 属性名称是否出现在 这个表的列明中
                    if (dr.Table.Columns.Contains(p.Name))
                    {
                        //6.判断  列中的值是否为 “空”
                        if (dr[p.Name] == DBNull.Value)
                        {
                            continue;
                        }
                        Type pt = p.PropertyType;
                        // 对可空类型的处理
                        if (pt.IsGenericType && pt.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                        {
                            // 获取  可空类型 的 基类型
                            NullableConverter nc = new NullableConverter(pt);
                            pt = nc.UnderlyingType;
                        }
                        object tempValue = Convert.ChangeType(dr[p.Name], pt);
                        p.SetValue(temp, tempValue);
                    }
                }
    
    
    
    
    
                return temp;
            }
    
        }
  • 相关阅读:
    LVS DR模式的简单应用
    Linux新加硬盘如何识别 分区 持续挂载
    13周作业
    12周作业
    2次月考
    10周作业
    9周作业
    8周作业
    7周作业
    6周作业
  • 原文地址:https://www.cnblogs.com/kiamer2425/p/7882218.html
Copyright © 2011-2022 走看看