问题描述
System.Type
类型包含属性和。阅读完MSDN文档后,我得出结论:这两个属性都存在以检查某个类型是开放还是封闭的泛型类型。 然而,我看不出有什么区别在这两者之间,以及当你想使用另一个时。 Type.ContainsGenericParameters / code>是递归的:
var genericList = typeof(List<>) ;
var listOfSomeUnknownTypeOfList = genericList.MakeGenericType(genericList);
listOfSomeUnknownTypeOfList.IsGenericTypeDefinition; // => false
listOfSomeUnknownTypeOfList.ContainsGenericParameters; // => true
这里发生的是 listOfSomeUnknownTypeOfList
是不是泛型类型定义本身,因为它的类型参数对于某些T而言是已知的 List< T>
。但是,由于 listOfSomeUnknownTypeOfList
不是完全已知的(因为它的类型参数是一个类型定义) ContainsGenericParameters
是 true
。
The System.Type
type contains the properties IsGenericTypeDefinition and ContainsGenericParameters. After reading the MSDN documentation I conclude that both properties exist to to check whether a type is an open or closed generic type.
However, I fail to see what the difference is between the two, and when you want to use one over the other.
Type.ContainsGenericParameters
is recursive:
var genericList = typeof(List<>);
var listOfSomeUnknownTypeOfList = genericList.MakeGenericType(genericList);
listOfSomeUnknownTypeOfList.IsGenericTypeDefinition; // => false
listOfSomeUnknownTypeOfList.ContainsGenericParameters; // => true
What happens here is that listOfSomeUnknownTypeOfList
is not a generic type definition itself because its type parameter is known to be a List<T>
for some T. However, since the type of listOfSomeUnknownTypeOfList
is not exactly known (because its type argument is a type definition) ContainsGenericParameters
is true
.
这篇关于Type.IsGenericTypeDefinition和Type.ContainsGenericParameters之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!