将扩展方法传递给期望委托的方法.这是如何运作的?

BFr*_*ree 11 c# extension-methods delegates

所以在工作中我使用的是我们没有编写的API,其中一个方法采用了委托.出于这样或那样的原因,我想到了我有一个适合该签名的扩展方法,所以我想知道它是否可行.我确信这不会让我感到意外,但确实如此.请允许我证明:

说我有这些课程:

public interface IMyInterface
{

}

public class MyClass : IMyInterface 
{ 

}

public static class Extensions
{
    public static string FuncMethod(this IMyInterface imy, int x)
    {
        return x.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在让我说我有一个方法签名,看起来像这样:

    private static void Method(Func<int, string> func)
    {

    }
Run Code Online (Sandbox Code Playgroud)

现在我的扩展方法(看起来像它)匹配该签名,但我们都知道扩展方法只是冒烟和镜像,所以它实际上与该签名匹配.然而,我可以安全地做到这一点:

var instance = new MyClass();
Method(instance.FuncMethod);
Run Code Online (Sandbox Code Playgroud)

我的问题是,这是如何工作的?编译器为我生成了什么以使其可接受.Extension方法的实际签名是一个实例IMyInterface,但Func事实并非在幕后发生了什么?

SLa*_*aks 11

实例方法实现为采用隐藏this参数.

从扩展方法创建实例委托时,隐藏this参数将作为第一个普通参数传递给方法.

请注意,使用值类型无法做到这一点.


Amy*_*y B 5

我不确切知道编译器在允许这些情况时会做什么,但是期望似乎是合理的。也许此代码示例将有助于解决这一概念。

MyClass instance = new MyClass();
Func<int, string> f1 = instance.FuncMethod;
Func<int, string> f2 = (i) => instance.FuncMethod(i);
Func<int, string> f3 = (i) => Extensions.FuncMethod(instance, i);
Run Code Online (Sandbox Code Playgroud)