通用方法存储在哪里? [英] Where are generic methods stored?

查看:108
本文介绍了通用方法存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关.NET的泛型的一些信息,并注意到了一件有趣的事情。

I've read some information about generics in .ΝΕΤ and noticed one interesting thing.

例如,如果我有一个泛型类:

For example, if I have a generic class:

class Foo<T> 
{ 
    public static int Counter; 
}

Console.WriteLine(++Foo<int>.Counter); //1
Console.WriteLine(++Foo<string>.Counter); //1

两个类 Foo< int> Foo< string> 在运行时不同。但是当非泛型类具有泛型方法时,情况又如何呢?

Two classes Foo<int> and Foo<string> are different at runtime. But what about case when non-generic class having generic method?

class Foo 
{
    public void Bar<T>()
    {
    }
}

显然,只有一个 Foo 类。但是方法 Bar 呢?所有通用类和方法都在运行时使用与其一起使用的参数关闭。这是否意味着类 Foo 具有 Bar 的许多实现,并且有关此方法的信息存储在内存中的位置? p>

It's obvious that there's only one Foo class. But what about method Bar? All the generic classes and methods are closed at runtime with parameters they used with. Does it mean that class Foo has many implementations of Bar and where the information about this method stored in memory?

推荐答案

与C ++模板相反,.NET泛型在运行时而不是在编译时进行评估。从语义上讲,如果使用不同的类型参数实例化泛型类,则它们的行为就像是两个不同的类,但实际上,在已编译的IL(中间语言)代码中只有一个类。

As opposed to C++ templates, .NET generics are evaluated in runtime, not at compile-time. Semantically, if you instantiate the generic class with different type parameters, those will behave as if it were two different classes, but under the hood, there is only one class in the compiled IL (intermediate language) code.

当您使用反射 typeof(YourClass< int>)将与 typeof(YourClass< string>)不同。这些称为构造的通用类型。还存在一个 typeof(YourClass<>),它表示通用类型定义。这是一些有关通过反射处理泛型的其他提示

The difference between different instantiatons of the same generic type becomes apparent when you use Reflection: typeof(YourClass<int>) will not be the same as typeof(YourClass<string>). These are called constructed generic types. There also exists a typeof(YourClass<>) which represents the generic type definition. Here are some further tips on dealing with generics via Reflection.

当您实例化构造的泛型类时,运行时会生成专门的上课。它与值类型和引用类型的工作方式之间存在细微的差异。

When you instantiate a constructed generic class, the runtime generates a specialized class on the fly. There are subtle differences between how it works with value and reference types.


  • 编译器只会在程序集中生成单个泛型类型。 / li>
  • 运行时会为您使用的每种值类型创建通用类的单独版本。

  • 运行时会为以下操作分配单独的一组静态字段

  • 由于引用类型的大小相同,因此运行时可以重用首次将其与引用类型一起使用时生成的专用版本。
  • >
  • The compiler will only generate a single generic type into the assembly.
  • The runtime creates a separate version of your generic class for each value type you use it with.
  • The runtime allocates a separate set of static fields for each type parameter of the generic class.
  • Because reference types have the same size, the runtime can reuse the specialized version it generated the first time you used it with a reference type.

对于通用方法,原理相同。


  • 编译器仅生成一个通用方法,即通用方法定义

  • 在运行时,该方法的每个不同的特殊化都被视为一个不同的方法o如果是同一个班级。

这篇关于通用方法存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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