C# c+中的静态方法/属性+;cli接口和实现类

C# c+中的静态方法/属性+;cli接口和实现类,c#,interface,static,c++-cli,C#,Interface,Static,C++ Cli,我正在尝试为我在c++cli中拥有的类创建一个接口,然后在c#中使用该类 基本上,我想做以下几点: public interface class IFoo { static int method(); }; public ref class Foo : public IFoo { static int method() { return 0; } }; 显然这是不正确的,因为在尝试编译时会出现错误。我试过很多不同的方法,但都没有用 在c#中,我将执行以下操作: publi

我正在尝试为我在c++cli中拥有的类创建一个接口,然后在c#中使用该类

基本上,我想做以下几点:

public interface class IFoo
{
   static int method();

};

public ref class Foo : public IFoo
{
   static int method() { return 0; }   
};
显然这是不正确的,因为在尝试编译时会出现错误。我试过很多不同的方法,但都没有用

在c#中,我将执行以下操作:

public interface IFooCSharp
{
    int method();
}

public class FooCSharp : IFooCSharp
{
   public static int method() { return 0 };

   int IFooSharp.method() { return FooCSharp.method(); }
}

所以我想看看是否有一种在c++cli中实现这一点的等效方法?

在接口中不能有静态成员

在C#中,您找到了正确的方法:通过显式接口实现,您只需使用for C++/CLI:

公共接口类IFoo
{
int方法();
};
public ref class Foo:public IFoo
{
静态int方法(){return 0;}
virtual int methodInterface()sealed=IFoo::method{return method();}
};
与C#不同,您需要为您的方法提供一个名称,即使您不打算直接使用它


以下是属性的语法:

public interface class IFoo
{
    property int prop;
};

public ref class Foo : public IFoo
{
    property int propInterface {
        virtual int get() sealed = IFoo::prop::get { return 0; }
        virtual void set(int value) sealed = IFoo::prop::set { /* whatever */ }
    };
};

接口中不能有静态成员

在C#中,您找到了正确的方法:通过显式接口实现,您只需使用for C++/CLI:

公共接口类IFoo
{
int方法();
};
public ref class Foo:public IFoo
{
静态int方法(){return 0;}
virtual int methodInterface()sealed=IFoo::method{return method();}
};
与C#不同,您需要为您的方法提供一个名称,即使您不打算直接使用它


以下是属性的语法:

public interface class IFoo
{
    property int prop;
};

public ref class Foo : public IFoo
{
    property int propInterface {
        virtual int get() sealed = IFoo::prop::get { return 0; }
        virtual void set(int value) sealed = IFoo::prop::set { /* whatever */ }
    };
};

太好了,谢谢。它提供了妨碍我的方法的名称。virtual int myprop1method()=IFoo::myProp1::get{return Foo::prop1;}@user2477533我在答案中添加了属性语法,我还添加了
sealed
以避免编译器警告(ref类或value类的私有虚拟方法应标记为“sealed”).太好了,谢谢。它提供了妨碍我的方法的名称。virtual int myprop1method()=IFoo::myProp1::get{return Foo::prop1;}@user2477533我在答案中添加了属性语法,我还添加了
sealed
以避免编译器警告(ref类或value类的私有虚拟方法应标记为“sealed”)。