【发布时间】:2010-09-12 17:03:11
【问题描述】:
我正在将一个用 C++ 编写的库翻译成 C#,并且关键字“union”存在一次。在结构中。
将它翻译成 C# 的正确方法是什么?它有什么作用?它看起来像这样;
struct Foo {
float bar;
union {
int killroy;
float fubar;
} as;
}
【问题讨论】:
-
这可能更安全,但是当您与提供此类数据结构的 C 库交互时,C# 中的此决定甚至会破坏 C/C++ 结构的基本封装。我正在尝试处理这样的事情: struct LibrarySType { AnotherType *anotherTypeBuff; int oneSetOfFlags; int anotherSetOfFlags;联合 { 结构 { int structMember1; ... } oneUseOfThisLibraryType;结构 { 字符 *structMember2; ... } anotherUseOfThisLibraryType; ... } 你; int64 *更多东西; ... 你明白了} 现在我没有发明这个聪明的数据结构,但它是我需要的供应商 API 的一部分