Type中的3个bool属性: IsGenericType , IsGenericTypeDefinition , IsGenericParameter
2024-08-22 21:35:34
- 首先说下 IsGenericType
用3个实例说明:
typeof(DateTime).IsGenericType : false
typeof(List<int>).IsGenericType: true
typeof(Dictionary<,>).IsGenericType:true
类型如果是泛型则为 true
但是要注意以下情况:
T[], List[] 等等数组时, IsGenericType为False
typeof(T[]).GetElementType().IsGenericType才是True
- 接着说 IsGenericTypeDefinition
用2个实例说明:
typeof(List<int>).IsGenericTypeDefintion : false
typeof(List<>).IsGenericTypeDefinition :true
IsGenericTypeDefinition : 获取一个值,该值指示当前 Type 是否表示可以用来构造其他泛型类型的泛型类型定义。
也就是说表明 这个 type 是否可以用于构建泛型定义
比如 List<> 可以通过反射构建出 List,List
例子:
var typeList = typeof(List<>);
Type typeDataList = typeList.MakeGenericType( typeof(DateTime)); //通过List<>构建出List<DateTime>
- 最后说下 IsGenericParameter
这个Property用于表明当前类型是一个T类型
例如:
typeof(List<>).GetGenericArguments()
返回: new Type[]{ typeof(T) }
此时:
typeof(T).IsGenericParameter == True
typeof(T).GenericParameterPosition == 0
对比:
typeof(List<DateTime>).GetGenericArguments()
返回: new Type[]{ typeof(DateTime) }
此时:
typeof(DateTime).IsGenericParameter == False
typeof(DateTime).GenericParameterPosition : throw exception
最新文章
- 工作中那些提高你效率的神器(第一篇)_Everything
- yii + elasticsearch 手册
- IOS第16天(4,Quartz2D柱状图)
- Window 常用文件
- poj3692 最大点权独立集/最大独立集
- shell基础(一)
- Firefox常用插件
- Android Studio下载安装使用教程
- 参数TFilterPredicate 类型说明
- SDUT 2351 In Danger
- ctr预估模型
- C#使用DirectoryEntry操作IIS创建网站和虚拟路径
- 【转】CodeGear RAD 2007 SP4
- 实参时丢弃了类型 discards qualifiers discards qualifiers问题
- js 设计模式之观察者模式
- Oracle EBS OM 取消订单行
- PHP代码审计笔记--命令执行漏洞
- HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]
- 下载YouTube视频的方法
- Python学习-29.Python中列表的一些操作