Reflection反射,Assembly程序集,Type类型类,AssemblyQualifiedName程序集的限定名,DataTable和IList接口互相转化

System.Type类:
System.Type 类对于反射起着核心的作用。但它是一个抽象的基类,Type有与每种数据类型对应的派生类,我们使用这个派生类的对象的方法、字段、属性来查找有关该类型的所有信息。
获取给定类型的Type引用有3种常用方式:
●使用 C# typeof 运算符。
Type t = typeof(string);
●使用对象GetType()方法。
string s = "grayworm";
Type t = s.GetType();
●还可以调用Type类的静态方法GetType()。
Type t = Type.GetType("System.String");

Type类的属性:
Name 数据类型名
FullName 数据类型的完全限定名(包括命名空间名)
Namespace 定义数据类型的命名空间名
IsAbstract 指示该类型是否是抽象类型
IsArray 指示该类型是否是数组
IsClass 指示该类型是否是类
IsEnum 指示该类型是否是枚举
IsInterface 指示该类型是否是接口
IsPublic 指示该类型是否是公有的
IsSealed 指示该类型是否是密封类
IsValueType 指示该类型是否是值类型

Type类的方法:
GetConstructor(), GetConstructors():返回ConstructorInfo类型,用于取得该类的构造函数的信息
GetEvent(), GetEvents():返回EventInfo类型,用于取得该类的事件的信息
GetField(), GetFields():返回FieldInfo类型,用于取得该类的字段(成员变量)的信息
GetInterface(), GetInterfaces():返回InterfaceInfo类型,用于取得该类实现的接口的信息
GetMember(), GetMembers():返回MemberInfo类型,用于取得该类的所有成员的信息
GetMethod(), GetMethods():返回MethodInfo类型,用于取得该类的方法的信息
GetProperty(), GetProperties():返回PropertyInfo类型,用于取得该类的属性的信息
可以调用这些成员,其方式是调用Type的InvokeMember()方法,或者调用MethodInfo, PropertyInfo和其他类的Invoke()方法。

 

 

DataTable和IList接口互相转化

DataTable转化为IList<T>:  

public IList<T> GetList<T>(DataTable table)
        {
            IList<T> list = new List<T>();
            T t = default(T);
            PropertyInfo[] propertypes = null;
            string tempName = string.Empty;
            foreach (DataRow row in table.Rows)
            {
                t = Activator.CreateInstance<T>();
                propertypes = t.GetType().GetProperties();
                foreach (PropertyInfo pro in propertypes)
                {
                    tempName = pro.Name;
                    if (table.Columns.Contains(tempName.ToUpper()))
                    {
                        object value = row[tempName];
                        pro.SetValue(t, value, null);
                    }
                }
                list.Add(t);
            }
            return list;
        }

IList转化为DataTable:

第一种:

List<info> infos = Dal.GetInfos();
DataTable dt = new DataTable();
dt.Columns.Add("cName");

foreach(var info in infos)
{
     DataRow dr = dt.NewRow();
     dr["cName"] = info.Name;
     dt.Add(dr);
}

第二种:

public static class DataTableExtensions
   {
       /// <summary>
       /// 转化一个DataTable
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="list"></param>
       /// <returns></returns>
       public static DataTable ToDataTable<T>(this IEnumerable<T> list)
       {
           //创建属性的集合
           List<PropertyInfo> pList = new List<PropertyInfo>();
           //获得反射的入口
           Type type = typeof(T);
           DataTable dt = new DataTable();
           //把所有的public属性加入到集合 并添加DataTable的列
           Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
           foreach (var item in list)
           {
               //创建一个DataRow实例
               DataRow row = dt.NewRow();
               //给row 赋值
               pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
               //加入到DataTable
               dt.Rows.Add(row);
           }
           return dt;
       }

       /// <summary>
       /// DataTable 转换为List 集合
       /// </summary>
       /// <typeparam name="TResult">类型</typeparam>
       /// <param name="dt">DataTable</param>
       /// <returns></returns>
       public static List<T> ToList<T>(this DataTable dt) where T : class, new()
       {
           //创建一个属性的列表
           List<PropertyInfo> prlist = new List<PropertyInfo>();
           //获取TResult的类型实例  反射的入口
           Type t = typeof(T);
           //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表
           Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
           //创建返回的集合
           List<T> oblist = new List<T>();

           foreach (DataRow row in dt.Rows)
           {
               //创建TResult的实例
               T ob = new T();
               //找到对应的数据  并赋值
               prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
               //放入到返回的集合中.
               oblist.Add(ob);
           }
           return oblist;
       }

       /// <summary>
       /// 将集合类转换成DataTable
       /// </summary>
       /// <param name="list">集合</param>
       /// <returns></returns>
       public static DataTable ToDataTableTow(IList list)
       {
           DataTable result = new DataTable();
           if (list.Count > 0)
           {
               PropertyInfo[] propertys = list[0].GetType().GetProperties();
               foreach (PropertyInfo pi in propertys)
               {
                   result.Columns.Add(pi.Name, pi.PropertyType);
               }

               for (int i = 0; i < list.Count; i++)
               {
                   ArrayList tempList = new ArrayList();
                   foreach (PropertyInfo pi in propertys)
                   {
                       object obj = pi.GetValue(list[i], null);
                       tempList.Add(obj);
                   }
                   object[] array = tempList.ToArray();
                   result.LoadDataRow(array, true);
               }
           }
           return result;
       }

       /**/
       /// <summary>
       /// 将泛型集合类转换成DataTable
       /// </summary>
       /// <typeparam name="T">集合项类型</typeparam>
       /// <param name="list">集合</param>
       /// <returns>数据集(表)</returns>
       public static DataTable ToDataTable<T>(IList<T> list)
       {
           return ToDataTable<T>(list, null);
       }

       /**/
       /// <summary>
       /// 将泛型集合类转换成DataTable
       /// </summary>
       /// <typeparam name="T">集合项类型</typeparam>
       /// <param name="list">集合</param>
       /// <param name="propertyName">需要返回的列的列名</param>
       /// <returns>数据集(表)</returns>
       public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
       {
           List<string> propertyNameList = new List<string>();
           if (propertyName != null)
               propertyNameList.AddRange(propertyName);

           DataTable result = new DataTable();
           if (list.Count > 0)
           {
               PropertyInfo[] propertys = list[0].GetType().GetProperties();
               foreach (PropertyInfo pi in propertys)
               {
                   if (propertyNameList.Count == 0)
                   {
                       result.Columns.Add(pi.Name, pi.PropertyType);
                   }
                   else
                   {
                       if (propertyNameList.Contains(pi.Name))
                           result.Columns.Add(pi.Name, pi.PropertyType);
                   }
               }

               for (int i = 0; i < list.Count; i++)
               {
                   ArrayList tempList = new ArrayList();
                   foreach (PropertyInfo pi in propertys)
                   {
                       if (propertyNameList.Count == 0)
                       {
                           object obj = pi.GetValue(list[i], null);
                           tempList.Add(obj);
                       }
                       else
                       {
                           if (propertyNameList.Contains(pi.Name))
                           {
                               object obj = pi.GetValue(list[i], null);
                               tempList.Add(obj);
                           }
                       }
                   }
                   object[] array = tempList.ToArray();
                   result.LoadDataRow(array, true);
               }
           }
           return result;
       }
   }

原文地址:https://www.cnblogs.com/joeylee/p/2755633.html