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

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

问题描述

我有一个库,由三部分组成。首先是原生的C ++,它提供了实际的功能。其次是一个C ++ / CLI包装/适配器的C ++库,简化了C#和C ++的过渡。最后,我有一个C#库,它通过C ++ / CLI适配器调用C ++库。

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.

现在有我有两套平行的枚举定义,一是存放在cs文件和其他在.h文件。这就带来了一个双重的问题:

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. 在我有双重维护。我必须在这两个文件中的位置同步枚举的变化。
  2. 在使用这两个枚举的命名空间应该是相同的,但在C ++ / CLI的包装,其中认为枚举两套以及它们之间的转换,即被命名冲突。

现在,我不知道一个解决方案,如 <一个href="http://blogs.msdn.com/b/abhinaba/archive/2007/08/27/sharing-enums-across-c-and-c.aspx">this <一个href="http://www.techtalkz.com/vc-net/67089-exposing-c-enum-managed-c-assembly.html#post283183">that将解决两个的问题。思考?

Right now I'm not sure a solution such as this or that would solve both problems. Thoughts?

推荐答案

只要把你的的#includeEnum.cs指令外的命名空间内,以解决命名冲突

Just put your #include "Enum.cs" directive inside an outer namespace to resolve the naming collision.

编辑:布伦特暗示的变化是使用的#define 替代的命名空间中的一个(或者甚至是枚举名称本身)在cs文件中声明。这也避免了命名冲突,而不会使命名空间层次结构更深。

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天全站免登陆