使用SWIG在接口中生成C#属性

使用SWIG在接口中生成C#属性,c#,interface,properties,swig,C#,Interface,Properties,Swig,我想使用swig3.0.12生成一个C#接口。对于公共功能来说,这是可以预期的。但是,如果我尝试向接口添加属性,它们就不是生成的接口的一部分 可以告诉SWIG向公共接口添加属性吗 下面是一个简单的例子: 我的C++类看起来像: class IA { public: virtual std::string GetX() = 0; virtual void SetX(const std::string& x) = 0; virtual void MyFuncti

我想使用swig3.0.12生成一个C#接口。对于公共功能来说,这是可以预期的。但是,如果我尝试向接口添加属性,它们就不是生成的接口的一部分

可以告诉SWIG向公共接口添加属性吗

下面是一个简单的例子:

<>我的C++类看起来像:

class IA
{
  public:
    virtual std::string GetX() = 0;
    virtual void SetX(const std::string& x) = 0;

    virtual void MyFunction() = 0;
};
public virtual void MyFunction() {
    examplePINVOKE.IA_MyFunction(swigCPtr);
}

public string X {
    set {
        examplePINVOKE.IA_X_set(swigCPtr, value);
        if (examplePINVOKE.SWIGPendingException.Pending) throw examplePINVOKE.SWIGPendingException.Retrieve();
    }
    get {
        string ret = examplePINVOKE.IA_X_get(swigCPtr);
        if (examplePINVOKE.SWIGPendingException.Pending) throw examplePINVOKE.SWIGPendingException.Retrieve();
        return ret;
    } 
}
public interface IASwigInterface {
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    global::System.Runtime.InteropServices.HandleRef GetInterfaceCPtr();
    void MyFunction();
}
使用以下接口文件:

%module example

%{
#include "IA.h"
%}

%include <std_string.i>
%include <attribute.i>
%include <swiginterface.i>

%attribute(IA, std::string, X, GetX, SetX);
%interface(IA);

%include "IA.h"
生成的IASwigInterface.cs如下所示:

class IA
{
  public:
    virtual std::string GetX() = 0;
    virtual void SetX(const std::string& x) = 0;

    virtual void MyFunction() = 0;
};
public virtual void MyFunction() {
    examplePINVOKE.IA_MyFunction(swigCPtr);
}

public string X {
    set {
        examplePINVOKE.IA_X_set(swigCPtr, value);
        if (examplePINVOKE.SWIGPendingException.Pending) throw examplePINVOKE.SWIGPendingException.Retrieve();
    }
    get {
        string ret = examplePINVOKE.IA_X_get(swigCPtr);
        if (examplePINVOKE.SWIGPendingException.Pending) throw examplePINVOKE.SWIGPendingException.Retrieve();
        return ret;
    } 
}
public interface IASwigInterface {
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    global::System.Runtime.InteropServices.HandleRef GetInterfaceCPtr();
    void MyFunction();
}
我希望SWIG为IASwigInterface.cs生成如下内容:

public interface IASwigInterface {
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    global::System.Runtime.InteropServices.HandleRef GetInterfaceCPtr();
    void MyFunction();
    string X { get; set; } //<- I want the property to be part of the interface
}
公共接口{
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
全局::System.Runtime.InteropServices.HandleRef GetInterfaceCPtr();
void MyFunction();

字符串x{get;set;}//i知道这不能回答您的问题,但是如果您需要通过C++访问的类和接口的数量不是太大,您可能想考虑使用C++/CLI来为C++代码创建托管包装。