从C#,C ++ / CLI和C ++共享一个枚举 [英] Sharing an enum from C#, C++/CLI, and C++
问题描述
现在我有两套并行枚举定义,一个存储在.cs文件,另一个在.h文件中。这带来了双重问题:
- 我有双重维护。我必须始终同步两个文件位置的枚举更改。
- 两个枚举使用的命名空间应该是相同的,但是C ++ / CLI包装器,它会同时查看两组枚举,并在它们之间进行转换导致命名冲突。
现在我不确定一个解决方案,如 此 或 将解决两个问题。想想?
只要把你的 #includeEnum.cs
指令在外部命名空间中解决命名冲突。
编辑:Brent建议的变体是使用 #define
替换在.cs文件中声明的其中一个命名空间(甚至枚举名称本身)。这也避免了命名冲突,而不会使命名空间层次更深。
I have a library that consists of three parts. First is native C++, which provides the actual functionality. Second is a C++/CLI wrapper/adaptor for the C++ library, to simplify the C# to C++ transition. Finally I have a C# library, which invokes the C++ library through the C++/CLI adaptor.
Right now there I have two sets of parallel enum definitions, one stored in a .cs file and the other in a .h file. This poses a double problem:
- I have dual maintenance. I must always synchronize changes of an enum in both file locations.
- The namespace used by both enums should be identical but the C++/CLI wrapper, which views both sets of enums and translates between them, incurs a naming collision.
Right now I'm not sure a solution such as this or that would solve both problems. Thoughts?
Just put your #include "Enum.cs"
directive inside an outer namespace to resolve the naming collision.
EDIT: A variation suggested by Brent is to use #define
to substitute one of the namespaces (or even the enum name itself) declared in the .cs file. This also avoids the naming collision, without making the namespace hierarchy deeper.
这篇关于从C#,C ++ / CLI和C ++共享一个枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!