Is it possible to share an enum declaration between C# and unmanaged C++?

后端 未结 5 1563
遥遥无期
遥遥无期 2020-12-05 07:39

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

I have the following enum used in completely unmanaged code:



        
5条回答
  •  时光取名叫无心
    2020-12-05 08:39

    I've had the same problem in the past and solved it using preprocessor definitions.

    In your unmanaged code, inside a header that can also be included by your managed wrapper, place your enumeration items into a #define.

    Then, in your managed and unmanaged enumeration definitions, use the same #define for both usages.

    The movement of the enumerations between the managed and unmanaged world looks slightly nasty (basically a cast is needed), but to your caller in the unmanaged world, it'll look and feel fine.

    Good luck,

提交回复
热议问题