简体   繁体   中英

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?

Given:

C++:

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

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

I would like to output 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. My attempts only yielded generated C# with no public constructors.

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 .
  • 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.

Also take a look at this post: 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

"Interface file" and then "Building a C# module" are the imp things there. Below is mentioned for compile/link in the same document. Do the same in 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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