Search code examples
c#c++code-translation

Converting simple C++ code to C# automatically


I have a file in C++ containing constant definitions, I want to use the same definitions in a C# project. Since both projects are part of a bigger project, I want if there is a change (addition/deletion) in the C++ file it should get reflected in corresponding C# file too. I want to keep the 2 files in sync. I was wondering if there is a script/tool to do this.

A reverse solution (C#-->C++) would also work.

Clarification:

Currently the code is:

//C++ 
    struct Colors{ 
         static const int Red = 100; //Custom Values are important 
         static const int Green = 101; } 
//C#

public enum Color{ Red = 100; Green =101; }

Now I want to have a single file so that any changes in C++ are reflected in C# (or other way around) so that I can have a single file across the projects for these constants.

As you see, I want to map bunch of constants defined in a struct in C++ to a enum in C#. I want to make no/minimal changes in above expected definitions as there is other code dependent (in both projects) on the above structures (but might do it, if there's not a good way of accomplishing this in the current format)


Solution

  • You probably wont find a script... You should have your own script to do this. Otherwise MACROs are the best fit...
    If you have a script then you can create a rule in your makefile that will automatically run this script whenever you build your project.