我学了.NET的反思和我有一个很难搞清楚的差异。
I'm Studying up on .net reflection and am having a hard time figuring out the difference.
据我了解,名单,其中,T>
是一个泛型类型定义。这是否意味着,到.NET反射T是泛型类型?
From what I understand, List<T>
is a generic type definition. Does that mean that to .net reflection T is the generic type?
具体而言,我想我在寻找的Type.IsGenericType和Type.IsGenericTypeDefinition功能更多的背景。
Specifically, I guess I'm looking for more background on the Type.IsGenericType and Type.IsGenericTypeDefinition functions.
谢谢!
在您的例子名单,其中,T&GT;
是一个泛型类型定义。 T
被称为泛型类型参数。当类型参数中指定像名单,其中,串&GT;
或名单,其中,INT&GT;
或名单&LT;双&GT;
那么你有一个泛型类型。可以看到,通过运行一些code这样的...
In your example List<T>
is a generic type definition. T
is called a generic type parameter. When the type parameter is specified like in List<string>
or List<int>
or List<double>
then you have a generic type. You can see that by running some code like this...
public static void Main()
{
var l = new List<string>();
PrintTypeInformation(l.GetType());
PrintTypeInformation(l.GetType().GetGenericTypeDefinition());
}
public static void PrintTypeInformation(Type t)
{
Console.WriteLine(t);
Console.WriteLine(t.IsGenericType);
Console.WriteLine(t.IsGenericTypeDefinition);
}
这将打印
System.Collections.Generic.List`1[System.String] //The Generic Type.
True //This is a generic type.
False //But it isn't a generic type definition because the type parameter is specified
System.Collections.Generic.List`1[T] //The Generic Type definition.
True //This is a generic type too.
True //And it's also a generic type definition.
另一种方式来获得泛型类型定义直接为的typeof(名单&LT;&GT;)
或 typeof运算(字典&LT;,&GT;)
。