Will IsConstructedGenericType always be the negation of IsGenericTypeDefinition, for a generic type?

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

The documentation of the instance property Type.IsConstructedGenericType is unclear or misleading.

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);     } }

The code runs through a Cartesian product consisting of 36 types t.

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.

For 35 types, IsGenericTypeDefinition was false while IsConstructedGenericType was true. For the last type, namely (unsurprisingly):

System.Collections.Generic.Dictionary`2[TKey,TValue]

the IsGenericTypeDefinition was true and IsConstructedGenericType was false.

Can I conclude that, for a generic type, the value of IsConstructedGenericType is always the opposite (negation) of IsGenericTypeDefinition?

(The documentation seems to claim that IsConstructedGenericType is instead the opposite of ContainsGenericParameters, but we clearly exhibited a lot of counterexamples to that.)

回答1:

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; }  } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!