插入空的c#类
SWIG and C#: insert empty C# class
本文关键字:插入 更新时间:2023-10-16
是否有一种方法(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 {
}
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 插入或删除时获取usb的dos_name
- 叮叮当当在修复时插入多个"覆盖"说明符
- 链表c++插入,所有情况都已检查,但没有任何工作
- 将重物插入std::map
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 在数字之间插入 + 或 - 符号以使其等于整数
- 在字符串中插入空格
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?