如何,何时何地都通用的方法,提出了具体的? [英] How, when and where are generic methods made concrete?

查看:117
本文介绍了如何,何时何地都通用的方法,提出了具体的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href="http://stackoverflow.com/questions/18257044/using-a-generic-type-argument-in-place-of-an-argument-of-type-system-type-is-it/19409760#19409760">This问题让我知道在哪里的一般方法的具体implementaiton实际上开始存在。我已经尝试了谷歌,但我不能想出正确的搜索。

This question got me wondering about where the concrete implementaiton of a generic method actually comes into existence. I've tried the google but am not coming up with the right search.

如果我们把这个简单的例子:

If we take this simple example:

class Program
{
    public static T GetDefault<T>()
    {
        return default(T);
    }

    static void Main(string[] args)
    {
        int i = GetDefault<int>();
        double d = GetDefault<double>();
        string s = GetDefault<string>();
    }
}

在我的脑子里,我一直认为,在某些时候它的结果与3所需的具体实现的实现,这样,在天真假的mangling,我们有这样的逻辑具体implementaiton其中特定类型的使用结果正确堆栈分配等。

in my head I've always assumed that at some point it results in an implementation with the 3 necessary concrete implementations such that, in naive pseudo mangling, we would have this logical concrete implementaiton where the specific types used result in the correct stack allocations etc.

class Program
{
    static void Main(string[] args)
    {
        int i = GetDefaultSystemInt32();
        double d = GetDefaultSystemFloat64();
        string s = GetDefaultSystemString();
    }

    static int GetDefaultSystemInt32()
    {
        int i = 0;
        return i;
    }
    static double GetDefaultSystemFloat64()
    {
        double d = 0.0;
        return d;
    }
    static string GetDefaultSystemString()
    {
        string s = null;
        return s;
    }
}

看一下IL为通用程序仍然是pssed泛型类型的条款:EX $ P $:

Looking at the IL for the generic program it is still expressed in terms of generic types:

.method public hidebysig static !!T  GetDefault<T>() cil managed
{
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] !!T CS$1$0000,
           [1] !!T CS$0$0001)
  IL_0000:  nop
  IL_0001:  ldloca.s   CS$0$0001
  IL_0003:  initobj    !!T
  IL_0009:  ldloc.1
  IL_000a:  stloc.0
  IL_000b:  br.s       IL_000d
  IL_000d:  ldloc.0
  IL_000e:  ret
} // end of method Program::GetDefault

那么,如何以及在什么点决定了一个int,然后双然后一个字符串必须分配在堆栈上,并返回给调用者?这是JIT处理的动作?难道我看这完全错误的光?

So how and at what point is it decided that an int, and then a double and then a string have to be allocated on the stack and returned to the caller? Is this an operation of the JIT process? Am I looking at this in the completely wrong light?

推荐答案

在C#中,泛型类型和方法的概念是由运行时本身的支持。 C#编译器并不需要真正创建一个通用的方法的具体版本。

In C#, the concepts of generic types and methods is supported by the runtime itself. The C# compiler does not need to actually create a concrete version of a generic method.

实际的具体通用方法在运行时创建由JIT,并在与IL不存在。在第一时间的通用方法用于一个类型,JIT将会看到,如果它被创建真实的,如果不是,构造适当的方法是通用的类型。

The actual "concrete" generic method is created at runtime by the JIT, and does not exist in the IL. The first time a generic method is used with a type, the JIT will see if it's been created, and if not, construct the appropriate method for that generic type.

这是仿制药之类的东西在C ++模板之间的根本区别之一。这也是主要原因许多与仿制药的限制 - 因为编译器不实际创建运行时实现的类型,接口限制是由编译时间的限制,这使得仿制药而言比用C模板多一点限制++处理潜在的用例。但是,它们在运行时本身所支持的事实允许创建不支持C ++和其他编译时创建的模板实现一般类型和从方式可能库的使用。

This is one of the fundamental differences between generics and things like templates in C++. It's also the main reason for many of the limitations with generics - since the compiler isn't actually creating the runtime implementation for types, the interface restrictions are handled by compile time constraints, which make generics a bit more limiting than templates in C++ in terms of potential use cases. However, the fact that they are supported in the runtime itself allows creation of generic types and usage from libraries possible in ways that aren't supported in C++ and other compile-time created template implementations.

这篇关于如何,何时何地都通用的方法,提出了具体的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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