Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create .Net extension methods by C++/CLI?

Tags:

People also ask

How do you declare an extension method in C#?

string s = "Hello Extension Methods"; int i = MyExtensions. WordCount(s); The preceding C# code: Declares and assigns a new string named s with a value of "Hello Extension Methods" .

Can we create extension method in non static class C#?

You can't do this with static classes, as you can't pass a static class as a parameter to a method. This isn't relevant to the question asked.

How do you implement and call a custom extension method?

To define and call the extension methodDefine a static class to contain the extension method. The class must be visible to client code. For more information about accessibility rules, see Access Modifiers. Implement the extension method as a static method with at least the same visibility as the containing class.

Can we create extension method for abstract class in C#?

The problem here is not that you can't add an extension method to an abstract class (you can - you can add an extension method to any type) - it's that you can't add a static method to a type with extension methods. Extension methods are static methods that present themselves in C# as instance methods.


In C#, extension methods can be created by

public static class MyExtensions {     public static ReturnType MyExt(this ExtType ext) {         ...     } } 

Since all of my library are written in C++/CLI, I would like to create the .net extension methods also in C++/CLI (in order to have one DLL instead of two). I've tried the following code

static public ref class MyExtensions { public:     static ReturnType^ MyExt(this ExtType ^ext) {         ...     } }; 

But the compiler can not recognize keyword 'this' in the first argument.

error C2059: syntax error: 'this' 

Is there some way to create the extension method in C++/CLI ?