如何在C ++ / CLI中声明一个在C#中被视为扩展方法的方法?

时间:2022-09-01 13:44:33

I'm writing a .NET assembly in C++/CLI to be used in our C#-based application. I'd like the application to see some of the C++ methods as extension methods. Is there some attribute I can apply to the declaration to specify that a method should be seen as an extension method from C#?

我正在用C ++ / CLI编写.NET程序集,以便在我们基于C#的应用程序中使用。我希望应用程序将一些C ++方法看作扩展方法。是否有一些属性可以应用于声明,以指定方法应该被视为C#的扩展方法?

3 个解决方案

#1


Make it a static method in a non-nested, non-generic static class, with the Extension attribute applied to both the class and the method.

使其成为非嵌套非泛型静态类中的静态方法,并将Extension属性应用于类和方法。

The tricky bit here may be the concept of a static class - that may not exist in C++/CLI... I'm not sure. It's possible that the C# compiler doesn't really care whether or not the class is static when detecting an extension method, only when declaring one. It's certainly worth a try without it.

这里棘手的一点可能是静态类的概念 - 在C ++ / CLI中可能不存在......我不确定。有可能C#编译器在检测扩展方法时并不真正关心该类是否是静态的,仅在声明扩展方法时。没有它,这当然值得一试。

#2


I had the same problem and here is a little example of an extension method in C++/CLI:

我遇到了同样的问题,这是C ++ / CLI中扩展方法的一个小例子:

using namespace System;

namespace CPPLib 
{
    [System::Runtime::CompilerServices::Extension]
    public ref class StringExtensions abstract sealed
    {
    public: 
        [System::Runtime::CompilerServices::Extension]
        static bool MyIsNullOrEmpty(String^ s)
        {
            return String::IsNullOrEmpty(s);
        }
    };
}

This extension method can be used just like any other in C#:

这个扩展方法可以像C#中的任何其他方法一样使用:

using CPPLib;

namespace CShartClient
{
    class Program
    {
        static void Main( string[] args )
        {

            string a = null;
            Console.WriteLine( a.MyIsNullOrEmpty() ? "Null Or Empty" : "Not empty" );
            a = "Test";
            Console.WriteLine( a.MyIsNullOrEmpty() ? "Null Or Empty" : "Not empty" );
            Console.ReadKey();
        }
    }
}

Unfortunately it seems C++ does not provide us with the "syntactic sugar" like in C#. At least I did not find it.

不幸的是,似乎C ++并没有像C#那样为我们提供“语法糖”。至少我没有找到它。

#3


I don't think you can. Extension methods are merely syntactic sugar, converted to static methods by the compiler.

我认为你不能。扩展方法只是语法糖,由编译器转换为静态方法。

#1


Make it a static method in a non-nested, non-generic static class, with the Extension attribute applied to both the class and the method.

使其成为非嵌套非泛型静态类中的静态方法,并将Extension属性应用于类和方法。

The tricky bit here may be the concept of a static class - that may not exist in C++/CLI... I'm not sure. It's possible that the C# compiler doesn't really care whether or not the class is static when detecting an extension method, only when declaring one. It's certainly worth a try without it.

这里棘手的一点可能是静态类的概念 - 在C ++ / CLI中可能不存在......我不确定。有可能C#编译器在检测扩展方法时并不真正关心该类是否是静态的,仅在声明扩展方法时。没有它,这当然值得一试。

#2


I had the same problem and here is a little example of an extension method in C++/CLI:

我遇到了同样的问题,这是C ++ / CLI中扩展方法的一个小例子:

using namespace System;

namespace CPPLib 
{
    [System::Runtime::CompilerServices::Extension]
    public ref class StringExtensions abstract sealed
    {
    public: 
        [System::Runtime::CompilerServices::Extension]
        static bool MyIsNullOrEmpty(String^ s)
        {
            return String::IsNullOrEmpty(s);
        }
    };
}

This extension method can be used just like any other in C#:

这个扩展方法可以像C#中的任何其他方法一样使用:

using CPPLib;

namespace CShartClient
{
    class Program
    {
        static void Main( string[] args )
        {

            string a = null;
            Console.WriteLine( a.MyIsNullOrEmpty() ? "Null Or Empty" : "Not empty" );
            a = "Test";
            Console.WriteLine( a.MyIsNullOrEmpty() ? "Null Or Empty" : "Not empty" );
            Console.ReadKey();
        }
    }
}

Unfortunately it seems C++ does not provide us with the "syntactic sugar" like in C#. At least I did not find it.

不幸的是,似乎C ++并没有像C#那样为我们提供“语法糖”。至少我没有找到它。

#3


I don't think you can. Extension methods are merely syntactic sugar, converted to static methods by the compiler.

我认为你不能。扩展方法只是语法糖,由编译器转换为静态方法。