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

查看:37
本文介绍了究竟什么是“开放泛型类型"?在.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 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.

封闭类型是一种非开放类型.

因此 TListDictionaryDictionary 都是开放类型(TU 是类型参数)而 ListDictionary 是封闭类型.

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() 以外的表达式中,并且您不能实例化它或调用它的方法.例如,List<>Dictionary<,> 是未绑定类型.

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]

这是 List 的 CLR 名称.在运行时很清楚类型参数是 System.Int32.这使得 List 成为 bound 开放类型.

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.

在运行时,您可以使用反射将类型参数绑定到未绑定泛型类型的未指定类型参数,使用 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.");

您可以检查类型是否是未绑定的泛型类型(泛型类型定义),您可以使用 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

要在运行时从构造类型获取未绑定类型,您可以使用 Type.GetGenericTypeDefinition 方法.

To get the unbound type from a constructed type at runtime, you can use the Type.GetGenericTypeDefinition method.

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

请注意,对于泛型类型,您可以拥有完全未绑定的类型定义,或完全绑定的定义.您不能绑定某些类型参数而让其他类型参数未绑定.例如,您不能有 DictionaryDictionary<,string>.

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

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

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