实现C#接口的C ++头文件

时间:2021-04-10 15:08:23

It is possible to generate a C++ header file from a C# interface?

可以从C#接口生成C ++头文件吗?

Example:

C# Interface:

public interface A { 
    void M( ushort u ); 
}

C++ header:

public ref class B : A 
{ 
    void M( unsigned short u ); 
}

Returns "Error C3766".

返回“错误C3766”。

2 个解决方案

#1


You have to use the virtual keyword. This compiled as expected:

您必须使用virtual关键字。这按预期编译:

public ref class B : ClassLibrary1::A {
public:
    virtual void M(unsigned short u) {}
};

Where ClassLibrary1 was the namespace in which the C# interface declaration was made.

其中ClassLibrary1是进行C#接口声明的命名空间。

#2


The interface must also be public. Try this instead (note 2nd public keyword):

界面也必须是公共的。试试这个(注意第二个公共关键字):

public ref class B : public A { void M( unsigned short u ); }

#1


You have to use the virtual keyword. This compiled as expected:

您必须使用virtual关键字。这按预期编译:

public ref class B : ClassLibrary1::A {
public:
    virtual void M(unsigned short u) {}
};

Where ClassLibrary1 was the namespace in which the C# interface declaration was made.

其中ClassLibrary1是进行C#接口声明的命名空间。

#2


The interface must also be public. Try this instead (note 2nd public keyword):

界面也必须是公共的。试试这个(注意第二个公共关键字):

public ref class B : public A { void M( unsigned short u ); }