c# - 在 C++ 中定义的以下结构的 C# 等价物是什么

标签 c# c++ dll struct

我在我的一个项目中使用了以下用 C++ 定义的结构,这些结构将在 Unity 引擎的 C# 脚本中重新用于另一个项目。我希望将这些结构类型用作 C# 脚本和 dll 中的 C 函数之间的参数类型,但我不知道如何将它们转换为 C# 声明。

结构定义如下:

struct SMove
{
    std::string m_MotionID;
    std::string m_AnimSrc;
    float           m_StartFrame;
    float           m_EndFrame;

// features

enum XFEATURE
{
    XFEATURE_NONE = 0,

    // Insert new features
    XFEATURE_ENERGY,            
    XFEATURE_POWERLEVEL,            

};

float           m_Intensity;
};

struct SElement
{
    float           m_Start;
    float           m_End;
};
typedef std::vector<SElement> TElements;

struct SGroup
{
    float           m_Start;
    float           m_End;
    long            m_Level;

    TElements       m_Elements;
};
typedef std::vector<SGroup> TGroups;

struct SDivision
{
    std::string m_PlayerID;
    std::string m_DivisionID;
    float           m_Start;
    float           m_End;

    TGroups     m_Groups;

    // features
    float           m_Intensity;

};
typedef std::vector<SDivision> TDivisions;

typedef std::vector<long*>      TScript;

struct SScriptList
{
    TScript* m_Seg[9][4][2];
};

我刚刚开始学习 C#,我知道它只能与 C 互操作,因此所有 std::string 和 std::vector 都必须以某种方式替换为 C 和 C# 都认可的东西。我知道 std::string 可以替换为 const char* 但我该如何转换其余部分?我用谷歌搜索了很多网站,但找不到任何与我的相似的例子。如何在 C# 及其 C 等价物(如 SDivision 和 SGroup 中)中的另一个结构内声明结构数组?

更新:

我已经将一些结构转换为 C,如下所示:

struct SElement
{
    float           m_Start;
    float           m_End;
};

struct SGroup
{
    float           m_Start;
    float           m_End;
    long            m_Level;

    //Array of Elements     
    SElement*       m_pElements;
    int             m_numElements;
};

struct SDivision    {
    const char*     m_PlayerID;
    const char*     m_DivisionID;
    float           m_Start;
    float           m_End;

    //Array of Groups
    SGroup*     m_pGroups;
    int             m_numGroups;

    float           m_Intensity;
};

C# 的等价物是什么?

最佳答案

这里最好的做法是使用 C++/CLI 桥接 DLL。 C++/CLI 桥应使用与构建原始 DLL 的相同版本的 Visual Studio 构建。构建结果是常规的 .NET 库。

关于c# - 在 C++ 中定义的以下结构的 C# 等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6041478/

相关文章:

c# - Entity Framework - 更改默认值

c# - 如何正确包装 Dictionary<T,U> 并公开枚举器?

c# - Java 中有类似 Enumerable.Range(x,y) 的东西吗?

c++ - 访问 QApplication::arguments 时出现段错误

c++ - vector 的 vector 的初始化是否可以进行范围构造?

c# 添加打开 wpf 程序的虚拟打印机?

windows - 系统 DLL 地址空间

c# - 异步方法不适用于 ObjectResult<T>

python - 使用 boost python : introductory example not working 从 python 调用 C++

c++ - 如何在 dll (c++) 中创建线程?