究竟什么是"开放式泛型类型"在.NET? [英] What exactly is an "open generic type" in .NET?

查看:459
本文介绍了究竟什么是"开放式泛型类型"在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过 Asp.Net MVC课程得知,对于方法有资格作为操作的控制器,

I was going through Asp.Net MVC lesson and learned that, for a method to qualify as an action for a controller,

  • 在它不能有一个开放式泛型类型

我明白有些仿制药,并利用它们在一定程度上,但是:

I understand generics somewhat and use them to some extent, but:

  • 什么是开放式泛型类型在.NET。
  • 是否有这样的事,作为一个封闭式泛型类型
  • 打开泛型类型是不是经常使用的一个术语。什么是使用/混淆呢?
  • What is an open generic type in .Net.
  • Is there such a thing as a closed generic type?
  • Open generic type is a term not used very often. What is used / confused with it ?

推荐答案

C#语言定义了一个开放式是一个类型,是任何一个类型参数或未知类型的参数定义的泛型类型:

The C# language defines an open type to be a type that's either a type argument or a generic type defined with unknown type arguments:

所有类型可以被分类为两种类型的打开或关闭的类型。一个的开放式的是,涉及类型参数的类型。更具体地讲:

All types can be classified as either open types or closed types. An open type is a type that involves type parameters. More specifically:

      
  • 系统类型参数定义了一个开放类型。
  •   
  • 数组类型是开放型,当且仅当其元素类型是开放类型。
  •   
  • A 构造类型的是一个开放式当且仅当它的一个类型变量或者更多的是一种的开放式的。 A 构造嵌套类型的是一个开放式当且仅当它的一个类型参数或其包含的类型(S)的类型,一个或多个参数是一个开放式。
  •   
  • A type parameter defines an open type.
  • An array type is an open type if and only if its element type is an open type.
  • A constructed type is an open type if and only if one or more of its type arguments is an open type. A constructed nested type is an open type if and only if one or more of its type arguments or the type arguments of its containing type(s) is an open type.

A 封闭式的是一个类型,它不是开放式。

A closed type is a type that is not an open type.

因此​​ T 名单,其中,T> 词典<字符串,T> 词典< T,U> 全部开放类型( T U 的类型参数),而名单,其中,INT> 词典<字符串,INT> 是封闭的类型。

Therefore T, List<T>, and Dictionary<string,T>, and Dictionary<T,U> are all open types (T and U are type arguments) whereas List<int> and Dictionary<string,int> are closed types.

有一个相关的概念:一个的未绑定的泛型类型的是一个泛型类型不明类型的参数。未绑定类型不能比其他的typeof() EX pressions使用,你不能实例,或调用它的方法。例如,名单,其中;&GT; 词典&LT;,&GT; 是绑定类型

There's a related concept: An unbound generic type is a generic type with unspecified type arguments. An unbound type can't be used in expressions other than typeof() and you can't instantiate it or call its methods. For instance, List<> and Dictionary<,> are unbound types.

要澄清一个开放型和未绑定类型之间的微妙区别:

To clarify the subtle distinction between an open type and an unbound type:

class Program {
   static void Main() { Test<int>(); }
   static void Test<T>() {
      Console.WriteLine(typeof(List<T>)); // Print out the type name
   }
}

如果你运行这段代码,它会打印出

If you run this snippet, it'll print out

System.Collections.Generic.List`1[System.Int32]

这是CLR名称名单,其中,INT&GT; 。很明显,在运行时的类型参数是 System.Int32的。这使得名单,其中,T&GT; A的的约束的开放式

which is the CLR name for List<int>. It's clear at runtime that the type argument is System.Int32. This makes List<T> a bound open type.

在运行时,您可以使用反射类型参数绑定到绑定的泛型类型的未指定的类型参数的<一个href="http://msdn.microsoft.com/en-us/library/system.type.makegenerictype.aspx"><$c$c>Type.MakeGenericType方法:

At runtime, you can use reflection to bind type arguments to unspecified type parameters of unbound generic types with the Type.MakeGenericType method:

Type unboundGenericList = typeof(List<>);
Type listOfInt = unboundGenericList.MakeGenericType(typeof(int));
if (listOfInt == typeof(List<int>))
     Console.WriteLine("Constructed a List<int> type.");

您可以检查类型是绑定的泛型类型(泛型类型定义的),您可以从中构建绑定类型的<一个href="http://msdn.microsoft.com/en-us/library/system.type.isgenerictypedefinition.aspx"><$c$c>Type.IsGenericTypeDefinition物业:

You can check whether a type is an unbound generic type (generic type definition) from which you can construct bound types with the Type.IsGenericTypeDefinition property:

Console.WriteLine(typeof(Dictionary<,>).IsGenericTypeDefinition); // True
Console.WriteLine(typeof(Dictionary<int,int>).IsGenericTypeDefinition); // False

从构造类型在运行时获得的未绑定类型,您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition.aspx"><$c$c>Type.GetGenericTypeDefinition方法。

Type listOfInt = typeof(List<int>);
Type list = listOfInt.GetGenericTypeDefinition(); // == typeof(List<>)

请注意,对于一个泛型类型,你可以有一个完全非绑定类型定义,或完全绑定的定义。你不能绑定某种类型的参数,让别人绑定。举例来说,你不能有词典&LT; INT,&GT; 词典&LT;,串&GT;

Note that for a generic type, you can either have a completely unbound type definition, or a completely bound definition. You can't bind some type parameters and leave others unbound. For instance, you can't have Dictionary<int,> or Dictionary<,string>.

这篇关于究竟什么是&QUOT;开放式泛型类型&QUOT;在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆