【问题标题】:SWIG: wrap enum to c#SWIG:将枚举包装到 c#
【发布时间】:2017-06-30 13:59:33
【问题描述】:

我无法使用 SWIG 将 c++ 枚举成员包装到 C#。 c++ 枚举成员的大小超过 Int32。在 C# 中,枚举成员的默认类型是 Int32,因此我有编译错误。虽然我关注了instructions,但也解决不了问题。

MyClass.h

class  MyClass
{
public:
    MyClass() {}
    ~MyClass() {}
    enum  BigNumbers
    {
        big = 0x80000000, bigger
    };
};

MyClass.i

%module cpp

%{
    #include "MyClass.h"
%}
%include "MyClass.h"

%typemap(csbase) BigNumbers "uint"
%inline %{
  enum BigNumbers { big=0x80000000, bigger };
%}

结果我在生成的 c# 包装器中得到关注成员,这导致编译错误cannot implicitly convert type 'uint' to 'int'

MyClass.cs

  public enum BigNumbers {
    big = 0x80000000,
    bigger
  } 

和单独文件中的全局 uint 枚举:

BigNumbers.cs

public enum BigNumbers : uint {
  big = 0x80000000,
  bigger
}

虽然我想以 MyClass 成员的身份在 MyClass.cs 中获得它。

请大家帮帮我!

【问题讨论】:

  • 您可以为enum指定底层类型
  • 我只是在学习 SWIG。你能多写一点吗?
  • 我对 SWIG 一无所知,但您可以像这样定义枚举类型 public enum MyEnum : long { whatever = long.MaxValue }
  • 是的!这正是我想从 C++ 代码中的枚举中自动获得的,但不能

标签: c# c++ enums swig


【解决方案1】:

抱歉这个问题很愚蠢,我应该更好地阅读手册。
正确的接口文件内容如下:

MyClass.i

%module cpp

%{
    #include "MyClass.h"

%}
%typemap(csbase) somens::MyClass::BigNumbers "uint"
%include "MyClass.h"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    相关资源
    最近更新 更多