gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:49:57 26 4
gpt4 key购买 nike

这是我的 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 docs :

For enumerations, it is critical that the original enum definition be included somewhere in the interface file (either in a header file or in the %{,%} block). SWIG only translates the enumeration into code needed to add the constants to a scripting language. It needs the original enumeration declaration to retrieve the correct enum values.

确保您正在“解析”头文件并将其包含在内:

 %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

关于c# - 如何使用 SWIG 从 C 代码生成的 C# 中的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49279814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com