Translate C ++ / CLI to C #

How can I translate a small C ++ / CLI project to C #

+2


source to share


3 answers


One carousel, manual way is to compile your C ++ / CLI project and open the output assembly in Reflector . Parse each class, convert the disassembled IL to C #, and save that code.

As for an automatic way to do this, I can't think of anything off my head.



Those things that say do you really want to want to convert your project to C #? If your C ++ / CLI project uses any kind of unmanaged code, you will have a tough time with a purely managed equivalent. If the project is more or less composed of pure CLR code, and it was written in C ++ / CLI to write in C ++ / CLI, then I can figure out I want to convert it to C #. But if there is a reason for writing it in C ++ / CLI, you can keep it that way.

+6


source


IMHO, line by line is the best way. I've ported several C ++ style projects to a managed language and I've tried different approaches; translators, in turn, scripts, etc. Over time, I found the most efficient way to do it one at a time, although it seems to be the slowest at first.

Too much is lost in the translator. The translator is not perfect and you end up spending a lot of time fixing the translated code. Also, translated code tends to be ugly and tends to be less readable than manual code. So the result is a fixed, not very pretty code base.



A few tips I have on the line

  • Start by defining all types of sheets
  • For every type that has a non-trivial (memory-freeing) destructor, implements IDisposable
  • Include a FxCop rule that checks for no Dispose calls to catch all locations using the used stack based RAII and skip it.
  • Pay special attention to the use of byref in C ++.
+4


source


I haven't tried it, but I just searched for it and found this: http://code2code.net/ According to this, you shouldn't completely rely on the code it produces:

You agree that this page only does half the work. Further work is required on your part. In most cases, the translated code won't even compile.

Also read the following: Translate C ++ / CLI to C #

+1


source







All Articles