VS不允许我调用我的扩展方法

A.D*_*.D. -2 c# extension-methods

出于任何原因,VS允许我使用我的扩展方法调用的长写.但编译器拒绝较短的形式:

public static class OperationExtender {

public static void MyMethod(Operation ope, int value) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

}

我可以这样调用它:

OperationExtender.MyMethod(anOperation, 0);
Run Code Online (Sandbox Code Playgroud)

但如果我输入:

anOperation.OperationExtender(0);
Run Code Online (Sandbox Code Playgroud)

VS生成这个通常的编译错误:

 'OperationExtender.MyMethod(Operation, int)' 
 does not contain a definition for
'MyMethod' and no extension method 'MyMethod' accepting a first argument of type 'Operation' could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

如果第一次写作被接受,我认为它不能成为usung/NS /汇编问题....

Dav*_*vid 5

因为它不是扩展方法.您忘记了this第一个参数(类型为"扩展")的关键字:

public static void MyMethod(this Operation ope, int value) {
    //...
}
Run Code Online (Sandbox Code Playgroud)