![](/img/trans.png)
[英]Change return type of a function in WCF without changing interface return type
[英]Changing return type in generated CS function using SWIG
我正在嘗試在 C# 中為 C++ 庫生成包裝器並使用生成的 C3 包裝器我正在開發客戶端應用程序。
C++類:
namespace ns {
class ISampleInterface {
public:
virtual ~ISampleInterface() {}
virtual void fn(int cnt) = 0;
};
}
namespace ns {
class ISampleInterface2 {
public:
virtual ~ISampleInterface() {}
virtual void fn2(int cnt) = 0;
};
}
namespace ns {
class SampleClass {
public:
SampleClass() {}
~SampleClass() {}
void InputFunction(int count, ISampleInterface sampleInterface, ISampleInterface2& sampleInterface2){
}
ISampleInterface& OutputFunction(void) {
return null;
}
};
}
預期的 C# 代碼:
namespace csNS {
public class SampleClass {
public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
// some code
}
public SampleInterface OutputFunction() {
// some code
}
}
}
Swig 接口聲明:
%interface_custom("SampleInterfaceImpl", "SampleInterface", ns::ISampleInterface);
%interface_custom("SampleInterface2Impl", "SampleInterface2", ns::ISampleInterface2);
%Outparams(ns:ISampleInterface, SampleInterfaceImpl)
%Outparams(ns:ISampleInterface2, SampleInterface2Impl)
在上面的 C# 代碼中, InputFunction 接受 3 個參數。 但是,為 SampleInterface 和 SampleInterface2 生成的實現不包含默認構造函數。 因此我無法為這些接口創建對象。 由於我無法在 C# 端實例化接口,我想將 then 作為 out 參數傳遞給 CS 函數,然后將在 C++ 端初始化。 因此,要修改在我定義的 typemap 下面的 swig 編譯期間生成的 cs 文件。
類型圖聲明:
%define %Outparams(TYPE, InterfaceImpl)
%typemap (cstype) TYPE,
TYPE &,
TYPE *,
TYPE *& "out TYPE"
%enddef
使用上面的 typemap 我可以更改輸入參數,但是,它也在更改函數返回值,我無法控制這一點。
生成的實際 cs 代碼:
namespace csNS {
public class SampleClass {
public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
// some code
}
public out SampleInterface OutputFunction() {
// some code
}
}
}
我究竟做錯了什么???
我更改了 typemap 聲明,它起作用了。
上課:
%typemap (cstype, out="TYPE") TYPE,
TYPE &,
TYPE *,
TYPE *& "out TYPE"
對於接口:
%typemap (cstype, out="InterfaceImpl") TYPE,
TYPE &,
TYPE *,
TYPE *& "out InterfaceImpl"
CS文件:
namespace csNS {
public class SampleClass {
public void InputFunction(int count, out SampleInterface sampleInterface, out SampleInterface2 sampleInterface2) {
// some code
}
public SampleInterfaceImpl OutputFunction() {
// some code
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.