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

时间:2017-06-28 11:57:53

标签: 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#中,我会做以下事情:

public interface IFooCSharp
{
    int method();
}

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

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

所以我希望看看在c ++ cli中是否有相同的方法可以做到这一点?

1 个答案:

答案 0 :(得分:3)

您无法在界面中拥有静态成员。

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

public interface class IFoo
{
    int method();
};

public ref class Foo : public IFoo
{
    static int method() { 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 */ }
    };
};
相关问题