是否可以在 C# 和非托管 C++ 之间共享枚举声明?
有没有办法在本机(非托管)C++ 和(托管)C# 之间共享枚举定义?
我在完全非托管代码中使用了以下枚举:
enum MyEnum { myVal1, myVal2 };
我们的应用程序有时使用托管组件。 该 C# 组件通过托管 C++ 互操作 DLL(来自本机 DLL)获取整数形式的枚举项值。 (互操作 dll 仅在需要 C# 组件时加载。)C# 组件重复了枚举定义:
public enum MyEnum { myVal1, myVal2 };
是否有办法消除重复而不将本机 C++ dll 转换为托管 dll?
Is there a way to share an enum definition between native (unmanaged) C++ and (managed) C#?
I have the following enum used in completely unmanaged code:
enum MyEnum { myVal1, myVal2 };
Our application sometimes uses a managed component. That C# component gets the enum item values as ints via a managed C++ interop dll (from the native dll). (The interop dll only loads if the C# component is needed.) The C# component has duplicated the enum definition:
public enum MyEnum { myVal1, myVal2 };
Is there a way to eliminate the duplication without turning the native C++ dll into a managed dll?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用单个 .cs 文件并在两个项目之间共享它。 在 .cs 文件上的 C++ 中的
#include
应该没有问题。这将是一个示例 .cs 文件:
如果您想在一个文件中包含多个枚举,您可以执行以下操作(尽管您必须暂时将 public 定义为对于 C / C++ 来说什么都不是):
You can use a single .cs file and share it between both projects.
#include
in C++ on a .cs file should be no problem.This would be an example .cs file:
If you want multiple enums in one file, you can do this (although you have to temporarily define public to be nothing for C / C++):
感谢分享!
我尝试了一下,找到了一种无需大量额外行即可拥有多个枚举和常量声明的方法:)
请记住将文件命名为 *.cs
Thanks for sharing!
I played around a bit and found a way to have multiple enum and constant declarations without having tons of extra lines :)
Remember to name your file *.cs
您可以将 C# 库公开给 COM,然后将类型库导入到非托管代码中 - 这样您就可以在非托管库中使用 C# 库中定义的枚举。
You can expose the C# library to COM and then import the type library into the unmanaged code - this way you will be able to use the enum defined in C# library in the unmanaged library.
非托管 C++ 和 C# 生活在两个不同的世界,因此,如果不将 C++ DLL 更改为托管 DLL,就无法使用相同的枚举。
即使这样,您可能也需要托管 C++ DLL 中的重复项。
C++ 枚举很像常量列表,而 C# 枚举继承了 Enum 类,因此提供了相当多的“技巧”。 正如您所看到的,它们非常不同。
如果本机 C++ DLL 是本机的还是托管的并不重要,我会将其转换为托管的,并将本机调用包装在托管 C++ 层中。
这样你就可以在 C++ DLL 中进行枚举重复,并且还可以同时摆脱所有互操作:-)
Unmanaged C++ and C# live in two different worlds, so no there is no way to use the same enum, without changing the c++ DLL into a managed one.
And even then, you'd probably need the duplication in the managed C++ DLL.
A C++ enum is much like a list of constants, whereas a C# enum inherits the Enum class, and thus provides quite a few "tricks". So as you can see, they're very different.
If it doesn't matter whether the native C++ DLL is native or managed, I'd turn it into a managed one and wrap the native calls inside a managed C++ Layer.
That way you can have the enum duplication inside the C++ DLL, and also you can get rid of all the interop at the same time :-)
我过去也遇到过同样的问题,并使用预处理器定义解决了它。
在非托管代码中,在托管包装器也可以包含的标头内,将枚举项放入 #define 中。
然后,在托管和非托管枚举定义中,对两种用法使用相同的#define。
托管和非托管世界之间的枚举移动看起来有点令人讨厌(基本上需要强制转换),但对于非托管世界中的调用者来说,它看起来和感觉都很好。
祝你好运,
I've had the same problem in the past and solved it using preprocessor definitions.
In your unmanaged code, inside a header that can also be included by your managed wrapper, place your enumeration items into a #define.
Then, in your managed and unmanaged enumeration definitions, use the same #define for both usages.
The movement of the enumerations between the managed and unmanaged world looks slightly nasty (basically a cast is needed), but to your caller in the unmanaged world, it'll look and feel fine.
Good luck,