将IsConstructedGenericType永远是IsGenericTypeDefinition的否定,对一个泛型类型? [英] Will IsConstructedGenericType always be the negation of IsGenericTypeDefinition, for a generic type?
问题描述
在实例属性的<一个文档href="http://msdn.microsoft.com/en-us/library/system.type.isconstructedgenerictype.aspx"><$c$c>Type.IsConstructedGenericType$c$c>不清楚或误导性的。
The documentation of the instance property Type.IsConstructedGenericType
is unclear or misleading.
我尝试以下code找到这个实际行为与相关属性:
I tried the following code to find the actual behavior of this and related properties:
// create list of types to use later in a Dictionary<,>
var li = new List<Type>();
// two concrete types:
li.Add(typeof(int));
li.Add(typeof(string));
// the two type parameters from Dictionary<,>
li.Add(typeof(Dictionary<,>).GetGenericArguments()[0]);
li.Add(typeof(Dictionary<,>).GetGenericArguments()[1]);
// two unrelated type parameters
li.Add(typeof(Func<,,,>).GetGenericArguments()[1]);
li.Add(typeof(EventHandler<>).GetGenericArguments()[0]);
// run through all possibilities
foreach (var first in li)
{
foreach (var second in li)
{
var t = typeof(Dictionary<,>).MakeGenericType(first, second);
Console.WriteLine(t);
Console.WriteLine(t.IsGenericTypeDefinition);
Console.WriteLine(t.IsConstructedGenericType);
Console.WriteLine(t.ContainsGenericParameters);
}
}
在code通过笛卡尔积组成的36种 T
。
结果:32种(所有,但4种组合词典&LT; INT,INT&GT;
,词典&LT; INT,字符串&GT;
,词典&LT;字符串,INT&GT;
,词典&LT;字符串,字符串&GT;
)的值 ContainsGenericParameters
是真实的。
Results: For 32 types (all but the 4 combinations Dictionary<int, int>
, Dictionary<int, string>
, Dictionary<string, int>
, Dictionary<string, string>
), the value of ContainsGenericParameters
was true.
有关35种, IsGenericTypeDefinition
是假的,而 IsConstructedGenericType
是真实的。对于最后一种,即(不出所料):
For 35 types, IsGenericTypeDefinition
was false while IsConstructedGenericType
was true. For the last type, namely (unsurprisingly):
System.Collections.Generic.Dictionary`2[TKey,TValue]
在 IsGenericTypeDefinition
是真实的 IsConstructedGenericType
是假的。
我可以断定,对于一个泛型类型,值 IsConstructedGenericType
总是 IsGenericTypeDefinition 相反(否定) code>?
Can I conclude that, for a generic type, the value of IsConstructedGenericType
is always the opposite (negation) of IsGenericTypeDefinition
?
(文档似乎宣称 IsConstructedGenericType
的而不是 ContainsGenericParameters
相反的,但我们清楚地展示了很多对反例的。)
(The documentation seems to claim that IsConstructedGenericType
is instead the opposite of ContainsGenericParameters
, but we clearly exhibited a lot of counterexamples to that.)
推荐答案
是的,这是正确的。假设键入
的问题是一个泛型类型,完全 IsGenericTypeDefinition
中的一个或 IsConstructedGenericType
是真实的。我们可以很容易地从参考源为 RuntimeType
(这是具体落实<$ C $的C>键入你的时候,你得到的GetType()
或 typeof运算
)为什么是这样的情况:
Yes this is correct. Assuming that the Type
in question is a generic type, exactly one of IsGenericTypeDefinition
or IsConstructedGenericType
is true. We can easily from the reference source for RuntimeType
(which is the concrete implementation of Type
you get when you do GetType()
or typeof
) why this is the case:
public override bool IsConstructedGenericType
{
get { return IsGenericType && !IsGenericTypeDefinition; }
}
这篇关于将IsConstructedGenericType永远是IsGenericTypeDefinition的否定,对一个泛型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!