2013-03-14 94 views
1

我在C++ CLI代碼:爲什麼C++ CLI不允許使用擴展方法?

[System::Runtime::CompilerServices::ExtensionAttribute] 
public ref class MyExtensions abstract sealed { 
public:   
    [System::Runtime::CompilerServices::ExtensionAttribute] 
    static System::String^ SetC() { 
     return gcnew System::String("{") + gcnew System::String("}") ; 
    } 
} 

但是下面一行拋出字符串沒有成員國家經貿委錯誤。

System::String("").SetC(); 

我也曾嘗試使用下面的代碼太:

gcnew System::String("")->SetC(); 

是什麼,錯過了什麼?

回答

8

C++/CLI將允許您使用擴展方法,但您必須將其稱爲常規靜態方法。有關示例,請參見this answer,使用Linq的First()作爲示例擴展方法進行調用。

+0

是的,工作正常。 – 2013-03-14 20:48:35

+0

還有一個問題,我作爲一個新的問題發佈.. http://stackoverflow.com/questions/15419974/why-c-sharp-change-my-color-structure-to-valuetype – 2013-03-14 20:55:10