Implementing an interface declared in C# from C++/CLI

好久不见. 提交于 2019-11-27 22:41:31
RandomNickName42
public ref class MyConcreteClass : public IMyInterface
{
 public:
  virtual void __clrcall Foo(String^ value) sealed;  

  virtual property String^ __clrcall MyProperty 
         { String^ get() sealed { String::Empty; } }
};

Interfaces need to be defined as virtual. Also note the "public IMy.." after the class decleration, it's a slighly different syntax than C#.

If you can, seal the interface members to improve performance, the compiler will be able to bind these methods more tightly than a typical virtual members.

Hope that helps ;)

I did not compile it but looks good to me... Oh and also, defining your methods as __clrcall eliminates dangers of double thunk performance penalties.

edit the correct syntax for a property is:

public ref class MyConcreteClass : public IMyInterface
{
 public:
  virtual property String^ MyProperty 
  {
    String^ get() sealed { return String::Empty; };
    void set( String^ s ) sealed { };
  }
};

or, when putting the definition in the source file:

public ref class MyConcreteClass : public IMyInterface
{
 public:
  virtual property String^ MyProperty 
  {
    String^ get() sealed;
    void set( String^ s ) sealed;
  }
};

String^ MyConcreteClass::MyProperty::get()
{
  return String::Empty;
}

void MyConcreteClass::MyProperty::set( String^ )
{
  //...
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!