泛型类型和泛型类型定义有什么区别?

时间:2021-10-04 20:11:58

I'm Studying up on .net reflection and am having a hard time figuring out the difference.

我正在研究.net反思,我很难搞清楚差异。

From what I understand, List<T> is a generic type definition. Does that mean that to .net reflection T is the generic type?

据我所知,List 是泛型类型定义。这是否意味着.net反射T是泛型类型?

Specifically, I guess I'm looking for more background on the Type.IsGenericType and Type.IsGenericTypeDefinition functions.

具体来说,我想我正在寻找有关Type.IsGenericType和Type.IsGenericTypeDefinition函数的更多背景知识。

Thanks!

谢谢!

2 个解决方案

#1


29  

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...

在您的示例中,List 是泛型类型定义。 T称为泛型类型参数。当类似参数在List 或List 或List 中指定时,则您具有泛型类型。您可以通过运行这样的代码来看到...

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

Which will print

哪个会打印

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.

Another way to get the generic type definition directly is typeof(List<>) or typeof(Dictionary<,>).

另一种直接获取泛型类型定义的方法是typeof(List <>)或typeof(Dictionary <,>)。

#2


1  

This will help explain it maybe:

这有助于解释它:

List<string> lstString = new List<string>();
List<int> lstInt = new List<int>();

if (lstString.GetType().GetGenericTypeDefinition() ==
    lstInt.GetType().GetGenericTypeDefinition())
{
    Console.WriteLine("Same type definition.");
}

if (lstString.GetType() == lstInt.GetType())
{
    Console.WriteLine("Same type.");
}

If you run it you will get the first test to pass because both items are implementing the type List<T>. The second test fails because List<string> is not the same as List<int>. The generic type definition is comparing the original generic before T is defined.

如果运行它,您将获得第一个要通过的测试,因为这两个项目都实现了List 类型。第二个测试失败,因为List 与List 不同。泛型类型定义在定义T之前比较原始泛型。

The IsGenericType type is just checking if the generic T has been defined. IsGenericTypeDefinition checks to see that the generic T has NOT been defined. This is useful if you want to know if two objects have been defined from the same base generic type such as the first List<T> example.

IsGenericType类型只是检查是否已定义泛型T. IsGenericTypeDefinition检查是否未定义泛型T.如果您想知道是否已从同一基本泛型类型(例如第一个List 示例)定义了两个对象,这将非常有用。

#1


29  

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...

在您的示例中,List 是泛型类型定义。 T称为泛型类型参数。当类似参数在List 或List 或List 中指定时,则您具有泛型类型。您可以通过运行这样的代码来看到...

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

Which will print

哪个会打印

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.

Another way to get the generic type definition directly is typeof(List<>) or typeof(Dictionary<,>).

另一种直接获取泛型类型定义的方法是typeof(List <>)或typeof(Dictionary <,>)。

#2


1  

This will help explain it maybe:

这有助于解释它:

List<string> lstString = new List<string>();
List<int> lstInt = new List<int>();

if (lstString.GetType().GetGenericTypeDefinition() ==
    lstInt.GetType().GetGenericTypeDefinition())
{
    Console.WriteLine("Same type definition.");
}

if (lstString.GetType() == lstInt.GetType())
{
    Console.WriteLine("Same type.");
}

If you run it you will get the first test to pass because both items are implementing the type List<T>. The second test fails because List<string> is not the same as List<int>. The generic type definition is comparing the original generic before T is defined.

如果运行它,您将获得第一个要通过的测试,因为这两个项目都实现了List 类型。第二个测试失败,因为List 与List 不同。泛型类型定义在定义T之前比较原始泛型。

The IsGenericType type is just checking if the generic T has been defined. IsGenericTypeDefinition checks to see that the generic T has NOT been defined. This is useful if you want to know if two objects have been defined from the same base generic type such as the first List<T> example.

IsGenericType类型只是检查是否已定义泛型T. IsGenericTypeDefinition检查是否未定义泛型T.如果您想知道是否已从同一基本泛型类型(例如第一个List 示例)定义了两个对象,这将非常有用。