Mohsen Sarkar
Mohsen Sarkar

Reputation: 6030

Why C++ CLI does not let to use extension methods?

My code in 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("}")  ;
    }
}

But following line throw an error that String has no member SetC.

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

I have also tried to use following code too :

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

Is anything missed ?

Upvotes: 1

Views: 3456

Answers (1)

David Yaw
David Yaw

Reputation: 27864

C++/CLI will let you use the extension methods, but you'll have to call it as a regular static method. See this answer for some examples, using Linq's First() as the example extension method to call.

Upvotes: 11

Related Questions