为什么 .NET 中的泛型类型在 TypeDef 中有一个条目,在 TypeSpec 中有另一个开放类型条目?
为什么 .NET 中的泛型类型在 TypeDef 中有一个条目,在 TypeSpec 中有另一个开放类型条目?
当泛型添加到 .NET 时,是否只是用签名来扩展 TypeDef?
Why does a generic type in .NET has one entry in the TypeDef and another open type entry in TypeSpec?
Was it simply to extended TypeDef with a signature when generics were added to .NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们看一下这两种不同的类型:
对于这两种定义,就像任何其他类型一样,TypeDef 表中会有一个条目。在这种情况下,GenericParam 表中也会有一个条目将 Foo 链接到 TFoo。
现在,大多数时候,当使用 Foo 时,您不会直接使用它的定义,因为它不是很有趣,您会想要使用 Foo 的实例化。因此,如果您编写:
TypeSpec 表中将会有一个条目,指定您正在使用带有一个泛型参数的 Foo 类型:字符串。如果您这样写,情况也是一样的:
编译器还必须创建 TypeSpec 条目,指定您使用带有一个泛型参数的 Foo 类型:TBar。您可以将 TypeSpec 视为类型的特化,但它们并不特定于泛型实例。如果你这样写:
你还会在 TypeSpec 表中找到一个条目,用来组成一个 Fighter 的矩形数组。但是如果你写:
将使用的 Foo 的定义,它不会被实例化:没有用于此用法的 TypeSpec 条目。另一方面,如果你写:
这里将使用我们之前讨论过的相同 TypeSpec。
要回答您的最后一个问题,在这种特殊情况下,并不是扩展 TypeDef 以添加通用信息:而是使用标准表来支持新类型构造(TypeSpec)。
另一方面,创建了一些其他表来处理新的泛型构造,例如 GenericParam,用于支持 TFoo 等泛型参数的定义,GenericParamConstraints 用于向泛型参数添加约束信息,以及 MethodSpec,即 Method What TypeSpec 相对于 TypeDef:一种从方法定义中使用专用方法的方法。方法签名也被修改为能够支持方法的通用数量。
Let's have a look at those two different types:
For those two definitions, and just like any other type, there will be an entry in the TypeDef table. In this case, there also will be an entry in the GenericParam table linking Foo to TFoo.
Now, most of the time, when using Foo, you won't use its definition directly, because it's not very interesting, you'll want to use an instantiation of Foo. So if you write:
There will be an entry in the TypeSpec table, specifying that you're using the type Foo with one generic argument: string. It's the same thing if you write:
The compiler will also have to create TypeSpec entry, specifying that you're using the type Foo with one generic argument: TBar. You can consider TypeSpec as a specialization of a type, but they're not particular to generic instances. If you write:
You'll also find a entry in the TypeSpec table, to compose a rectangular array of Fighter. But if you write:
The definition of Foo which will be used, it's not instantiated: no TypeSpec entry for this usage. On the other hand, if you write:
The same TypeSpec we talked about earlier will be used here.
To answer your last question, in this particular case, it's not to extend TypeDef to add generic informations: it's about using the standard tables to support new type constructs (TypeSpec).
On the other hand, some other tables were created to deal with new generic constructs, such as GenericParam, to support the definition of a generic parameter like TFoo, GenericParamConstraints to add constraints information to a generic parameter, and MethodSpec, which is to Method what TypeSpec is to TypeDef: a way to use a specialized method from a method definition. Also method signatures were modifyied to be able to support the generic arity of a method.