星期二, 九月 04, 2007

Sharing enums across C# and C++

 
 

Sent to you by Hudong via Google Reader:

 
 

via MSDN Blogs by abhinaba on Aug 27, 2007

On an internal DL someone asked a question about how folks share enums between C++ and C# code when required (e.g. common error codes for a large project that use both native code and C#).

There was a very interesting answer given by one person. He simply asked why don't you define it in a .cs file and #include that file in your C++ code. I initially thought what the hell is he talking about and then it struck me that C++ and C# is not just similar but in some context they are exactly the same. Consider the following enum definition in a .cs files

// File Enum.cs
namespace
EnumShare { enum MyEnum { a = 1, b = 2, c = 3, }; }

This can be simply included in a C# project to be built normally. However, it can also be pulled into C++ code as follows

#include "..\EnumShare\Enum.cs"  using namespace EnumShare;  int Foo()
{ cout << a << endl;

}

Note that I could directly pull in the C# code because the syntax matches that of C++ :)

I thought that this was a rather interesting usage.


 
 

Things you can do from here:

 
 

没有评论:

发表评论