是否有可能分享C#和非托管C ++的enum声明? [英] Is it possible to share an enum declaration between C# and unmanaged C++?

查看:227
本文介绍了是否有可能分享C#和非托管C ++的enum声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有分享本机(非托管)C ++和(托管)之间的枚举定义的方法C#?

Is there a way to share an enum definition between native (unmanaged) C++ and (managed) C#?

我在完全非托管code使用了以下枚举:

I have the following enum used in completely unmanaged code:

enum MyEnum { myVal1, myVal2 };

我们的应用程序有时会使用管理组件。那C#组件获得的枚举项值作为通过托管C ++互操作的dll整数(从本地的dll)。 (互操作的dll只有C#组件所需的负载。)的C#组件已复制了枚举定义:

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 };

有没有消除重复头也不回的原生C ++ DLL到托管DLL的方法吗?

Is there a way to eliminate the duplication without turning the native C++ dll into a managed dll?

推荐答案

您可以使用一个cs文件和两个项目之间共享。 的#include 在cs文件在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.

这将是一个例子.cs文件:

This would be an example .cs file:

#if !__LINE__    
namespace MyNamespace
{
    public 
#endif

// shared enum for both C, C++ and C#
enum MyEnum { myVal1, myVal2 };

#if !__LINE__
}
#endif

如果你想在一个文件中的多个枚举,你可以做到这一点(虽然你不得不暂时界定公共也没有什么的C / C ++):

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++):

#if __LINE__
#define public
#else
namespace MyNamespace
{
#endif

    public enum MyEnum { MyEnumValue1, MyEnumValue2 };
    public enum MyEnum2 { MyEnum2Value1, MyEnum2Value2 };
    public enum MyEnum3 { MyEnum3Value1, MyEnum3Value2 };

#if __LINE__
#undef public
#else
}
#endif

这篇关于是否有可能分享C#和非托管C ++的enum声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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