如何使用由SWIG从C代码生成的C#中的枚举?

如何使用由SWIG从C代码生成的C#中的枚举?,c#,c,swig,C#,C,Swig,这是我的C枚举: typedef enum { VALUE1, VALUE2, VALUE3 } myenum; 下面是我的C函数之一: myenum getEnumValue1() { return VALUE1; } 我本想在C#中得到这样的东西: 实际上,函数包装器是生成的,它返回一个名为myenum的东西,但我看不到VALUE1的提及,等等 如何使用C#包装器?我想进行比较、赋值、将这些值传递给函数等等,但我看不到任何方法 例如,我无法执行以下操作

这是我的C枚举:

typedef enum {
    VALUE1,
    VALUE2,
    VALUE3
} myenum;
下面是我的C函数之一:

myenum getEnumValue1() { 
    return VALUE1; 
}
我本想在C#中得到这样的东西:

实际上,函数包装器是生成的,它返回一个名为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
%模块示例
%{
/*在包装器代码中包含头*/
#包括“header.h”
%}
/*解析头文件以生成包装器*/

%包括“header.h”,在枚举中不使用
typedef
”的情况下尝试它。啊哈!现在生成C#enum。但是,它是空的:public enum SWIGTYPE_myenum{}谢谢,就是这样。我确实必须重构我的头文件,因为SWIG给了我臭名昭著的“Error:Syntax Error in input(1)”,原因在另一个枚举中未知(不是我试图获取的那个)。
 %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