简体   繁体   English

使用SWIG从C ++生成C#接口

[英]Generate C# Interface from C++ using SWIG

How do I use SWIG to generate a C# interface (or at least a C# mock-able base class) from C++ using SWIG? 如何使用SWIG从C ++使用SWIG生成C#接口(或至少是C#模拟基类)?

Given: 鉴于:

C++: C ++:

class IWidget
{
public:
     virtual void Flob() = 0;
};

class Widget : public IWidget
{
public:
     void Flob() {};
};

I would like to output C#: 我想输出C#:

public interface IWidget
{
     void Flob();
}

public class Widget : IWidget
{...}

Note: Solution does not have to be an interface, but I do need to be able to use mocking frameworks such as Moq or Rhino.Mocks to mock out the base of the C# Widget class. 注意:解决方案不一定是接口,但我需要能够使用模拟框架(如Moq或Rhino.Mocks)来模拟C#Widget类的基础。 My attempts only yielded generated C# with no public constructors. 我的尝试只产生了没有公共构造函数的生成的C#。

You may be able to get the output you list by using one or more of: 您可以使用以下一项或多项来获取列出的输出:

  • csclassmodifiers
  • use %ignore on the pure virtual methods, add necessary method declarations using cscode . 在纯虚方法上使用%ignore ,使用cscode添加必要的方法声明。
  • csinterfaces typemap
  • csinterfaces_derived typemap

However this may not be the easiest way to achieve the goal of making your class mockable. 但是,这可能不是实现使您的类可模拟的目标的最简单方法。 Consider extending your post to clearly identify what is the c# that you need to achieve this if interface is not used. 考虑扩展您的帖子以清楚地标识如果不使用interface ,您需要实现此目的的c#。

Also take a look at this post: http://swig.10945.n7.nabble.com/Multiple-inheritance-and-C-any-easy-workarounds-available-td11516.html . 另请看这篇文章: http//swig.10945.n7.nabble.com/Multiple-inheritance-and-C-any-easy-workarounds-available-td11516.html

Sorry I can't provide an actual code answer, hopefully the above gets you there. 对不起,我无法提供实际的代码答案,希望上面的内容可以帮助您。

Start with the 从...开始

Swig tutorial Swig教程

"Interface file" and then "Building a C# module" are the imp things there. “接口文件”然后“构建一个C#模块”就是那里的东西。 Below is mentioned for compile/link in the same document. 下面提到了同一文档中的编译/链接。 Do the same in Visual Studio. 在Visual Studio中执行相同的操作。

$ swig -csharp  example.i
$ gcc -c -fpic  example.c example_wrap.c
$ gcc -shared example.o  example_wrap.o   -o libexample.so
$ cscc -o runme *.cs 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM