C#中 IsGenericType和IsGenericTypeDefinition区别

IsGenericType tells you that this instance of System.Type represents a generic type with all its type parameters specified. For example, List<int> is a generic type.

IsGenericTypeDefinition, on the other hand, tells you that this instance of System.Type represents a definition from which generic types can be constructed by supplying type arguments for its type parameters. For example, List<> is a generic type definition.

You can get a generic type definition of a generic type by calling GetGenericTypeDefinition:

var listInt = typeof(List<int>);
var typeDef = listInt.GetGenericTypeDefinition(); // gives typeof(List<>)

You can make a generic type from a generic type definition by providing it with type arguments to MakeGenericType:

var listDef = typeof(List<>);
var listStr = listDef.MakeGenericType(typeof(string));

IsGenericType:

typeof(DateTime).IsGenericType : false

typeof(List<int>).IsGenericType: true

typeof(Dictionary<,>).IsGenericType:true

IsGenericTypeDefinition:

typeof(List<int>).IsGenericTypeDefintion : false

typeof(List<>).IsGenericTypeDefinition :true

IsGenericTypeDefinition : 获取一个值,该值指示当前 Type 是否表示可以用来构造其他泛型类型的泛型类型定义。 
也就是说表明 这个 type 是否可以用于构建泛型定义 
比如 List<> 可以通过反射构建出 List,List 

  1. var typeList = typeof(List<>);

  2. Type typeDataList = typeList.MakeGenericType( typeof(DateTime)); //通过List<>构建出List<DateTime>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值