从C#,C ++ / CLI和C ++共享一个枚举 [英] Sharing an enum from C#, C++/CLI, and C++

查看:157
本文介绍了从C#,C ++ / CLI和C ++共享一个枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图书馆由三部分组成。首先是本机C ++,它提供了实际的功能。二是用于C ++库的C ++ / CLI包装器/适配器,以简化C#到C ++转换。最后我有一个C#库,它通过C ++ / CLI适配器调用C ++库。



现在我有两套并行枚举定义,一个存储在.cs文件,另一个在.h文件中。这带来了双重问题:


  1. 我有双重维护。我必须始终同步两个文件位置的枚举更改。

  2. 两个枚举使用的命名空间应该是相同的,但是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:

  1. I have dual maintenance. I must always synchronize changes of an enum in both file locations.
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆