共享来自 C#、C++/CLI 和 C++ 的枚举 [英] Sharing an enum from C#, C++/CLI, and C++

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

问题描述

我有一个由三部分组成的库.首先是原生 C++,它提供了实际的功能.第二个是 C++ 库的 C++/CLI 包装器/适配器,以简化 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 包装器(它查看两组枚举并在它们之间进行转换)会导致命名冲突.

现在我不确定像 this 可以解决这两个问题.想法?

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

推荐答案

只需将 #include "Enum.cs" 指令放在外部命名空间内即可解决命名冲突.

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

Brent 建议的一种变体是使用 #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天全站免登陆