插入空的c#类
本文关键字:插入 | 更新日期: 2023-09-27 18:15:41
是否有一种方法(typemap或pragma)插入自定义c#类到生成的c#代码?使用%typemap(cscode)
可以很容易地将代码添加到包装的c++类中,但不清楚添加c#类…
例如,用于创建自定义异常的SWIG文档说:
除了手工制作的CustomApplicationException
外,还必须使用上述锅炉板代码:
// Custom C# Exception
public class CustomApplicationException : global::System.ApplicationException {
public CustomApplicationException(string message)
: base(message) {
}
}
但是不清楚如何通过接口文件添加这个类
有一个文档化的pragma,接近你想要的,%pragma(csharp) imclasscode
。这会将代码插入生成的modulePINVOKE.cs文件中。
在SWIG的源代码树中,我们发现:
那么,假设您对下面的嵌套类很满意:/* ----------------------------------------------------------------------------- * pragmaDirective() * * Valid Pragmas: * imclassbase - base (extends) for the intermediary class * imclassclassmodifiers - class modifiers for the intermediary class * imclasscode - text (C# code) is copied verbatim to the intermediary class * imclassimports - import statements for the intermediary class * imclassinterfaces - interface (implements) for the intermediary class * * modulebase - base (extends) for the module class * moduleclassmodifiers - class modifiers for the module class * modulecode - text (C# code) is copied verbatim to the module class * moduleimports - import statements for the module class * moduleinterfaces - interface (implements) for the module class * * ----------------------------------------------------------------------------- */ virtual int pragmaDirective(Node *n) {
%module test
%pragma(csharp) modulecode=%{
// Custom C# Exception
public class CustomApplicationException : global::System.ApplicationException {
public CustomApplicationException(string message)
: base(message) {
}
}
%}
在module.cs中做的正是你想要的。如果这还不够,那么我们必须开始寻找解决方案,它可能会变得更干净,更简单,只是在SWIG之外手工编写一小块独立的代码块(但在相同的源代码树中)。
如果你真的想进入变通的领域,你可以(ab)使用moduleclassmodifiers pragma向全局命名空间插入内容,例如:
%module test
%pragma(csharp) moduleclassmodifiers=%{
// Custom C# Exception
public class CustomApplicationException : global::System.ApplicationException {
public CustomApplicationException(string message)
: base(message) {
}
}
public class%}
生产test.cs:
using System;
using System.Runtime.InteropServices;
// Custom C# Exception
public class CustomApplicationException : global::System.ApplicationException {
public CustomApplicationException(string message)
: base(message) {
}
}
public class test {
}