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

后端 未结 5 1571
遥遥无期
遥遥无期 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:25

    You can use a single .cs file and share it between both projects. #include in C++ on a .cs file should be no problem.

    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
    

    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
    

提交回复
热议问题