.NET泛型术语 - 打开/关闭,未绑定/构造绑定、术语、NET

2023-09-03 09:33:52 作者:我的名字你的姓氏

.NET泛型的术语是有点暧昧。甚至更糟 - 它似乎是含糊的和不同使用不同的来源。什么基本上是不明确的(相对于类型)这4个方面的关系:

.NET generics terminology is a bit ambiguous. Even worse - it seems to be used ambiguously and differently in different sources. What basically is not clear is relationships between these 4 terms (in relation to "Type"):

开启 关闭 绑定 构造

据我所知,名单,其中,T> 是开放的,名单,其中,INT> 关闭。但真正的建造,而就打开/关闭类型绑定?

I understand that List<T> is open and List<int> is closed. But what really is "constructed" and "unbound" in relation to open/closed types?

推荐答案

在language规格:

4.4构造类型

一个泛型类型声明,其本身   表示的未绑定的泛型类型的   用作蓝图,以形成许多   不同的类型,通过施加方式   类型参数。该类型参数是   写尖括号内(小于和>)   紧接在通用类型的名称如下。 A型,其中包括   至少一种类型的参数被称为一个   构造类型。构造类型   在大多数地方,在使用   语言,其中一个类型名称可以   出现。未绑定泛型类型   仅在一个使用   typeof运算-EX pression(§7.6.11)。   [...]

A generic type declaration, by itself, denotes an unbound generic type that is used as a "blueprint" to form many different types, by way of applying type arguments. The type arguments are written within angle brackets (< and> ) immediately following the name of the generic type. A type that includes at least one type argument is called a constructed type. A constructed type can be used in most places in the language in which a type name can appear. An unbound generic type can only be used within a typeof-expression (§7.6.11). [...]

4.4.2开放和封闭类型

所有类型都可分为   打开类型或关闭类型。开放   类型是涉及类型的类型   参数。更具体地讲:

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.

•构造类型是开放的   键入当且仅当一个或多个其   类型参数是一个开放式。一个   构造嵌套类型是一个开放的   键入当且仅当一个或多个其   类型参数或类型参数   它包含类型(S)是一个开放   类型。

• 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 closed type is a type that is not an open type. [...]

4.4.3绑定和非绑定类型

术语未绑定类型是指非通用   键入或绑定的泛型类型。该   长期的绑定类型指   非泛型类型或构造   类型。未绑定类型是指   由一个类型声明中声明的实体。   未绑定泛型类型本身不是   一类型,并且不能被用作类型   一个变量,参数或者返回   值,或作为基型。唯一   构造一个绑定的泛型   可以被引用类型是将typeof   EX pression(§7.6.11)。

The term unbound type refers to a non-generic type or an unbound generic type. The term bound type refers to a non-generic type or a constructed type. An unbound type refers to the entity declared by a type declaration. An unbound generic type is not itself a type, and cannot be used as the type of a variable, argument or return value, or as a base type. The only construct in which an unbound generic type can be referenced is the typeof expression (§7.6.11).

下面是一个例子,我想到了:

Here's an example I thought of:

// Foo<T> is an unbound generic type.
class Foo<T> { .. } 

// Bar<K> is an unbound generic type.
// Its base-class Foo<K> is a constructed, open generic type.
class Bar<K> : Foo<K> { .. } 

// IntFoo is not a generic type.
// Its base-class Foo<int> is a constructed, closed generic type.
class IntFoo : Foo<int> { .. } 

这是一个尝试,以配合在与反射API,使用相关属性: IsGenericType IsGenericTypeDefinition ContainsGenericParameters

搞不明白这22条外汇术语,炒外汇赚钱概率很小

And here's an attempt to tie that in with the reflection API, using the relevant properties: IsGenericType, IsGenericTypeDefinition and ContainsGenericParameters

(这些测试不是100%$ P $每个种按语言规范的pdictive)。

(These tests are not 100% predictive of each "kind" as per the language spec).

+----------+---------------------+-----------+--------------+-------------------+
|   Name   |        Kind         | IsGenType | IsGenTypeDef | ContainsGenParams |
+----------+---------------------+-----------+--------------+-------------------+
| Foo<>    | Unbound             | TRUE      | TRUE         | TRUE              |
| Foo<>*   | Constructed, open   | TRUE      | FALSE        | TRUE              |
| Foo<int> | Constructed, closed | TRUE      | FALSE        | FALSE             |
| IntFoo   | Not generic         | FALSE     | FALSE        | FALSE             |
+----------+---------------------+-----------+--------------+-------------------+
* = Bar<>'s base type.