2017-04-14 69 views
-2

由於任何原因,VS允許我使用我的擴展方法調用的長文字。但是編譯器拒絕較短的形式:VS不允許我調用我的擴展方法

公共靜態類OperationExtender {

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

}

我可以調用它是這樣的:

OperationExtender.MyMethod(anOperation, 0); 

但如果我鍵入:

anOperation.OperationExtender(0); 

VS產生這種通常comilation錯誤:

'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 /裝配問題....

+3

你錯過了'this'前的'操作ope'參數,所以它不是一個擴展方法。 – CodeCaster

+0

是你的擴展方法在同一個命名空間嗎? – Adrian

+0

顯然我錯過了這個,thx CodeCaster :) –

回答

5

,因爲它不是一個擴展方法。你忘了第一個參數的this關鍵字(類型被「延長」):

public static void MyMethod(this Operation ope, int value) { 
    //... 
} 
+0

傻我!!!!!!! –