为什么 .NET 中的泛型类型在 TypeDef 中有一个条目,在 TypeSpec 中有另一个开放类型条目?

发布于 2024-10-14 23:03:49 字数 104 浏览 4 评论 0原文

为什么 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-10-21 23:03:50

让我们看一下这两种不同的类型:

public class Foo<TFoo> {}

public class Fighter {}

对于这两种定义,就像任何其他类型一样,TypeDef 表中会有一个条目。在这种情况下,GenericParam 表中也会有一个条目将 Foo 链接到 TFoo。

现在,大多数时候,当使用 Foo 时,您不会直接使用它的定义,因为它不是很有趣,您会想要使用 Foo 的实例化。因此,如果您编写:

new Foo<string> ();

TypeSpec 表中将会有一个条目,指定您正在使用带有一个泛型参数的 Foo 类型:字符串。如果您这样写,情况也是一样的:

public class Bar<TBar> {
    public Foo<TBar> Foo;
}

编译器还必须创建 TypeSpec 条目,指定您使用带有一个泛型参数的 Foo 类型:TBar。您可以将 TypeSpec 视为类型的特化,但它们并不特定于泛型实例。如果你这样写:

var type = typeof (Fighter[,]);

你还会在 TypeSpec 表中找到一个条目,用来组成一个 Fighter 的矩形数组。但是如果你写:

var type = typeof (Foo<>);

将使用的 Foo 的定义,它不会被实例化:没有用于此用法的 TypeSpec 条目。另一方面,如果你写:

var type = typeof (Foo<string>);

这里将使用我们之前讨论过的相同 TypeSpec。

要回答您的最后一个问题,在这种特殊情况下,并不是扩展 TypeDef 以添加通用信息:而是使用标准表来支持新类型构造(TypeSpec)。

另一方面,创建了一些其他表来处理新的泛型构造,例如 GenericParam,用于支持 TFoo 等泛型参数的定义,GenericParamConstraints 用于向泛型参数添加约束信息,以及 MethodSpec,即 Method What TypeSpec 相对于 TypeDef:一种从方法定义中使用专用方法的方法。方法签名也被修改为能够支持方法的通用数量。

Let's have a look at those two different types:

public class Foo<TFoo> {}

public class Fighter {}

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:

new Foo<string> ();

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:

public class Bar<TBar> {
    public Foo<TBar> Foo;
}

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:

var type = typeof (Fighter[,]);

You'll also find a entry in the TypeSpec table, to compose a rectangular array of Fighter. But if you write:

var type = typeof (Foo<>);

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:

var type = typeof (Foo<string>);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文