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

时间:2017-04-14 11:27:18

标签: c# extension-methods

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

public static class OperationExtender {

public static void MyMethod(Operation ope, int value) {
  ...
}

}

我可以这样调用它:

OperationExtender.MyMethod(anOperation, 0);

但如果我输入:

anOperation.OperationExtender(0);

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?)

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

1 个答案:

答案 0 :(得分:5)

因为它不是扩展方法。您忘记了第一个参数(类型为“扩展”)的[ERROR] /opt/stack/devstack/functions-common:602 git call failed: [git clone git://git.openstack.org/openstack/requirements.git /opt/stack/requirements --branch master] 关键字:

this