首页 > 分享 > 代码学习库—— 一 Type的使用

代码学习库—— 一 Type的使用

最新推荐文章于 2024-07-22 09:14:37 发布

一二十三(OCT) 于 2019-07-25 09:09:07 发布

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

相关知识

JavaScript导入js文件使用type='module'
自定义(滑动条)input[type=“range”]样式
python玫瑰花代码简单
python玫瑰花代码讲解
【机器学习】任务二:波士顿房价的数据与鸢尾花数据分析及可视化
Python花卉展——323行代码编写你的flowers库(7种花)
Python机器学习基础教程
开始学习 HTML
Python 常用的标准库以及第三方库有哪些?
Python画玫瑰花完整代码

网址: 代码学习库—— 一 Type的使用 https://m.huajiangbk.com/newsview659204.html

所属分类:花卉
上一篇: 【drawio笔记】素描风格
下一篇: nixos