Delegate caching behavior changes in Roslyn

后端 未结 2 823
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 06:43

Given the following code:

public class C
{
    public void M()
    {
        var x = 5;
        Action action = y => Console.WriteLine(y);
             


        
2条回答
  •  醉话见心
    2020-11-27 07:03

    Yes. The most important part is that the method containing lambda implementation is now an instance method.

    You can see a delegate as a middleman receiving an instance call through Invoke and dispatching that call according to the calling convention of the implementing method.

    Note that there are platform ABI requirements that specify how arguments are passed, how results are returned, what arguments are passed via registers and in which ones, how "this" is being passed and so on. Violating these rules may have bad impact on tools that rely on stack-walking, such as debuggers.

    Now, if the implementing method is an instance method, the only thing that needs to happen inside the delegate is to patch "this", which is the delegate instance at the time of Invoke, to be the enclosed Target object. At that point, since everything else is already where it needs to be, the delegate can jump directly to the implementing method body. In many cases this is noticeably less work than what would need to happen if the implementing method was a static method.

提交回复
热议问题