Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Even if you include the C# enum in your native C++ (as suggested in your <a href="http://blogs.msdn.com/b/abhinaba/archive/2007/08/27/sharing-enums-across-c-and-c.aspx" rel="nofollow noreferrer">first link</a>), both enums are not "the same", the C++ enum is nothing but a list of named integers, while the C# enum is derived from Enum. As a consequence, you get a collision in C++/CLI when trying to use them both. </p> <p>A possible solution is to use the preprocessor so that your C++/CLI assembly sees both enums in different namespaces:</p> <pre><code>// shared_enum.h #undef ENUMKEYWORD #undef ENUMNAMESPACE #ifdef MANAGED #define ENUMKEYWORD public enum class #define ENUMNAMESPACE EnumShareManaged #else #define ENUMKEYWORD enum #define ENUMNAMESPACE EnumShare #endif namespace ENUMNAMESPACE { ENUMKEYWORD MyEnum { a = 1, b = 2, c = 3, }; } </code></pre> <p>In your C++/CLI code, make an inclusion like that:</p> <pre><code>#undef MANAGED #include "shared_enum.h" #define MANAGED #include "shared_enum.h" </code></pre> <p>This gives you the availability to distinguish between those two kind of enums <code>EnumShare::MyEnum</code> or <code>EnumShareManaged::MyEnum</code> in your C++/CLI code.</p> <p>EDIT: just found <a href="https://stackoverflow.com/questions/1873339/c-cli-casting-from-unmanaged-enum-to-managed-enum">this SO post</a> showing the correct way to cast between unmanaged and managed enums, this surely will work here, too. For example, in the C++/CLI, the transition from managed to unmanaged enum can be done like this:</p> <pre><code>void MyWrapperClass::MyWrapperFunction(EnumShareManaged::MyEnum mx) { EnumShare::MyEnum nx = static_cast&lt;EnumShare::MyEnum&gt;(mx); // call a native function "func" func(nx); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload