Question

I was converting a Managed c++ project to a C# one. The C++ project includes a constants C++ header file which is an external dependency present outside of the project.

In the newly created C# file, is there a way to include this C++ header file? I dont want to redefine these constants in a C# file as changes by clients will take place on the C++ header file.

Was it helpful?

Solution

It's not possible to include it.

You have 2 options really

  • duplicate it for the managed layer, and maintain it in synch with the C++ header.
  • read and parse it at runtime, and use reflection in the C# parts that require those symbols.

As noted by others, you can automate the first one.

OTHER TIPS

If you have a huge amount of header files, you can take a look at SWIG: http://www.swig.org/

This will generate C# files from C/C++ header files.

For more info see also: http://www.swig.org/Doc2.0/SWIGDocumentation.html#CSharp

The results are quite impressive! But the naming is more C++ like, than C# style... but this was expected...

Unfortunately no, that isn't possible as the C# compiler doesn't understand what to do with .h files. Even it if did, it still illegal to have un-scoped variables declarations (constant or otherwise) in .NET.

You'll have to convert the file either by hand, or as mentioned in the comment by Joachim Pileborg, build a utility to auto-convert it to C# code for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top