2013-03-14 92 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