C# VS不允许我调用扩展方法

C# VS不允许我调用扩展方法,c#,extension-methods,C#,Extension Methods,由于任何原因,VS允许我使用扩展方法调用的长时间编写。但编译器拒绝使用较短的格式: 公共静态类OperationXtender{ public static void MyMethod(Operation ope, int value) { ... } } 我可以这样调用它: OperationExtender.MyMethod(anOperation, 0); 但如果我输入: anOperation.OperationExtender(0); VS生成此常见的关联错误: 'Oper

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

公共静态类OperationXtender{

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/assembly问题……

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


操作ope
参数之前,您缺少
,因此它不是一个扩展方法。您的扩展方法是否在同一命名空间中?显然,我缺少此项,thx CodeCaster:)在询问此问题之前,您在谷歌上搜索过吗?你会很快找到答案的。
public static void MyMethod(this Operation ope, int value) {
    //...
}