[英]How to work with enums in C# generated by SWIG from C code?
这是我的C枚举:
typedef enum {
VALUE1,
VALUE2,
VALUE3
} myenum;
这是我的C函数之一:
myenum getEnumValue1() {
return VALUE1;
}
我期待在C#中获得类似的信息:
public enum SWIGTYPE_p_myenum {
VALUE1,
VALUE2,
VALUE3
}
确实,函数包装器已生成,并且返回了称为myenum的内容,但是我看不到VALUE1等。
我如何使用C#包装器? 我想进行比较,赋值,将这些值传递给函数等,但是我看不到任何方法。
例如,我不能执行以下操作:
SWIGTYPE_p_myenum x = SWIGTYPE_p_myenum.VALUE1;
bool test = (x == SWIGTYPE_p_myenum.VALUE1);
只是没有包装器代码的生成使之成为可能。 这就是生成的全部内容:
public class SWIGTYPE_p_myenum {
private global::System.Runtime.InteropServices.HandleRef swigCPtr;
internal SWIGTYPE_p_myenum(global::System.IntPtr cPtr, bool futureUse) {
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
}
protected SWIGTYPE_p_myenum() {
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
}
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(SWIGTYPE_p_myenum obj) {
return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
}
}
从SWIG 文档 :
对于枚举,至关重要的是将原始枚举定义包含在接口文件中的某个位置(在头文件中或在%{,%}块中)。 SWIG仅将枚举转换为将常量添加到脚本语言所需的代码。 它需要原始的枚举声明来检索正确的枚举值。
确保您正在“解析”头文件,并包括它:
%module example
%{
/* Includes the header in the wrapper code */
#include "header.h"
%}
/* Parse the header file to generate wrappers */
%include "header.h" <== don't forget this part
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.