1.获取Type
有3种方式:
a.使用typeof运算符,如Type t = typeof(int);
b.使用GetType()方法,如int i;Type t = i.GetType();
c.使用Type类的静态方法GetType(),如Type t =Type.GetType(“System.Double”);
2.Type的属性:
Name:数据类型名;
FullName:数据类型的完全限定名,包括命名空间;
Namespace:数据类型的命名空间;
BaseType:直接基本类型;
UnderlyingSystemType:映射类型;
3.Type的方法:
GetMethod():返回一个方法的信息;
GetMethods():返回所有方法的信息。
GetMember()和GetMembers()方法返回数据类型的任何成员或所有成员的详细信息,不管这些成员是构造函数、属性和方法等。
GetProperties() 返回为当前 System.Type 的所有公共属性。
/// <summary> /// 获取实体模型队列 /// </summary> /// <typeparam name="T">类型</typeparam> /// <returns>实体队列</returns> public List<T> GetModelList<T>() { Type type = typeof(T); PropertyInfo[] propertys = type.GetProperties(); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Select "); foreach (PropertyInfo propertyInfo in propertys) { stringBuilder.AppendFormat(" {0},", propertyInfo.Name); } stringBuilder.Remove(stringBuilder.Length - 1, 1); stringBuilder.AppendFormat(" From {0} ", type.Name); List<T> list = new List<T>(); DataTable dataTable = GetDataTable(stringBuilder.ToString(), null); foreach (DataRow dataRow in dataTable.Rows) { T t = (T)Activator.CreateInstance(type); foreach (PropertyInfo propertyInfo in type.GetProperties()) { propertyInfo.SetValue(t,dataRow[propertyInfo.Name]); } list.Add(t); } stringBuilder.Clear(); stringBuilder = null; return list; } 12345678910111213141516171819202122232425262728293031323334
参考文档:
https://www.cnblogs.com/xcsn/p/9052330.html