C# 向SWIG中自动生成的类添加代码
我正试图找到一种方法,将代码添加到swig生成的函数中。我使用类型映射来扩展类,但在文档中找不到任何关于扩展特定函数的内容 给定以下swig接口文件:C# 向SWIG中自动生成的类添加代码,c#,c++,swig,dllimport,C#,C++,Swig,Dllimport,我正试图找到一种方法,将代码添加到swig生成的函数中。我使用类型映射来扩展类,但在文档中找不到任何关于扩展特定函数的内容 给定以下swig接口文件: %module Test %{ #include "example.h" %} %typemap(cscode) Example %{ bool 64bit = SizeOf(typeof(System.IntPtr)) == 8; static string Path = 64bit ? "/...Path to 64 bit
%module Test
%{
#include "example.h"
%}
%typemap(cscode) Example %{
bool 64bit = SizeOf(typeof(System.IntPtr)) == 8;
static string Path = 64bit ? "/...Path to 64 bit dll.../" :
"/...Path to 32 bit dll.../";
%}
%include "example.h"
%module Test
%{
#include "example.h"
%}
%typemap(csout,excode=SWIGEXCODE) SomeObject {
// Some extra stuff here
$&csclassname ret = new $&csclassname($imcall, true);$excode
return ret;
}
%include "example.h"
我得到了以下C#代码:
公共类MyClass:global::System.IDisposable{
...
bool 64bit=SizeOf(typeof(System.IntPtr))=8;
静态字符串路径=64位?/…64位dll的路径…/:
“/…32位dll的路径…/”;
...
在example.h中定义的公共静态SomeObject进程(…){//函数
本节介绍了如何使用
typemap(cscode)
扩展生成的类。您可以使用%typemap(csout)
生成所需的代码。不过,这有点麻烦,您需要复制csharp.swg中现有的SWIGTYPE类型映射(通用占位符)
例如,给定一个头文件example.h:
struct SomeObject {};
struct MyClass {
static SomeObject test();
};
然后可以编写以下SWIG接口文件:
%module Test
%{
#include "example.h"
%}
%typemap(cscode) Example %{
bool 64bit = SizeOf(typeof(System.IntPtr)) == 8;
static string Path = 64bit ? "/...Path to 64 bit dll.../" :
"/...Path to 32 bit dll.../";
%}
%include "example.h"
%module Test
%{
#include "example.h"
%}
%typemap(csout,excode=SWIGEXCODE) SomeObject {
// Some extra stuff here
$&csclassname ret = new $&csclassname($imcall, true);$excode
return ret;
}
%include "example.h"
产生:
public static SomeObject test() {
// Some extra stuff here
SomeObject ret = new SomeObject(TestPINVOKE.MyClass_test(), true);
return ret;
}
如果您想为所有返回类型生成它,而不仅仅是为返回某个对象的对象生成它,那么您需要为csout的所有变体做更多的工作。从未使用过swig(根据我刚才搜索的内容),似乎您想注入C特定的代码(
setdldirectory()
)在从预先存在的C++源文件中翻译时,如果是这样的话,那么我认为您必须重新排列C++代码以适应该注入。<代码> C++代码,你可以在C语言的后面重写。这可能是我想的那种方法,但是找不到一种方法来真正实现它。Flexo已经给出了一种直接注入C代码的替代方法,我将尝试它。它允许你添加额外的方法,但不能扩展现有的M。方法。我相信我看到过一篇文章,它使用了cscode
和%ignore
和%rename
,但我现在找不到它……这看起来很好,谢谢。我会试试看,看能不能一起得到一个工作版本。