What exactly is an quot;open generic typequot; in .NET?(究竟什么是“开放泛型?在.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.
封闭类型是一种非开放类型.
因此T
、List
、Dictionary
、Dictionary
T
和 U
是类型参数),而 List
和 Dictionary
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 名称.在运行时很明显 type 参数是 System.Int32
.这使得 List<T>
成为 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<>)
请注意,对于泛型类型,您可以有一个完全未绑定的类型定义,也可以有一个完全绑定的定义.您不能绑定某些类型参数而让其他类型参数未绑定.例如,您不能使用 Dictionary<int,>
或 Dictionary<,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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:究竟什么是“开放泛型"?在.NET 中?
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01