从 C++/CLI 实现用 C# 声明的接口
假设我有一个名为 IMyInterface
的 C# 接口,定义如下:
// C# code
public interface IMyInterface
{
void Foo(string value);
string MyProperty { get; }
}
假设我还有一个 C++/CLI 类 MyConcreteClass
,它实现此接口,其标头声明如下:
// C++/CLI header file
ref class MyConcreteClass : IMyInterface
{
public:
};
如何在 C++/CLI 标头中实现方法 Foo
和属性 MyProperty
?
我的尝试导致以下编译错误:
错误 C3766:“MyConcreteClass”必须 提供一个实现 接口方法'void IMyInterface::Foo(系统::字符串^ 值)'
Say I have a C# interface called IMyInterface
defined as follows:
// C# code
public interface IMyInterface
{
void Foo(string value);
string MyProperty { get; }
}
Assume I also have a C++/CLI class, MyConcreteClass
, that implements this interface and whose header is declared as follows:
// C++/CLI header file
ref class MyConcreteClass : IMyInterface
{
public:
};
How does one implement the method Foo
and the property MyProperty
in the C++/CLI header?
My attempt results in the following compile error:
error C3766: 'MyConcreteClass' must
provide an implementation for the
interface method 'void
IMyInterface::Foo(System::String^
value)'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
接口需要定义为虚拟的。 另请注意类声明后的“public IMy..”,它的语法与 C# 略有不同。
如果可以的话,密封接口成员以提高性能,编译器将能够比典型的虚拟成员更紧密地绑定这些方法。
希望有帮助;)
我没有编译它,但对我来说看起来不错...哦,还有,将你的方法定义为 __clrcall 可以消除双重重击性能损失的危险。
编辑
属性的正确语法是:
或者,将定义放入源文件时:
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:
or, when putting the definition in the source file: