How should I convert Java code to C# code?

前端 未结 6 1302
天命终不由人
天命终不由人 2021-01-12 19:43

I\'m porting a Java library to C#. I\'m using Visual Studio 2008, so I don\'t have the discontinued Microsoft Java Language Conversion Assistant program (JLCA).

My a

相关标签:
6条回答
  • 2021-01-12 20:11

    One more quick-and-dirty idea: you could use IKVM to convert the Java jar to a .NET assembly, then use Reflector--combined with the FileDisassembler Add-in--to disassemble it into a Visual C# project.

    (By the way, I haven't actually used IKVM--anyone care to vouch that this process would work?)

    0 讨论(0)
  • 2021-01-12 20:14

    I'm not sure if it is really the best way to convert the code line by line especially if the obstacles become overwhelming. Of course the Java code gives you a guideline and the basic structure but I think at the end the most important thing is that the library does provide the same functionality like it does in Java.

    0 讨论(0)
  • 2021-01-12 20:17

    Your doing it in the only sane way you can...the biggest help will be this document from Dare Obasanjo that lists the differences between the two languages:

    http://www.25hoursaday.com/CsharpVsJava.html

    BTW, change all getter and setter methods into properties...No need to have the C# library function just the same as the java library unless you are going for perfect interface compatibility.

    0 讨论(0)
  • 2021-01-12 20:25

    If you have a small amount of code then a line by line conversion is probably the most efficient.

    If you have a large amount of code I would consider:

    1. Looking for a product that does the conversation for you.
    2. Writing a script (Ruby or Perl might be a good candidate) to do the conversion for you - at least the monotonous stuff! It could be a simple search/replace for keyword differences and renaming of files. Gives you more time/fingers to concentrate on the harder stuff.
    0 讨论(0)
  • 2021-01-12 20:26

    I heard something the other day about a .net version of Java. If such a monster was available, would you just be able to compile the java source into .net intermediate libraries?

    0 讨论(0)
  • 2021-01-12 20:30

    Couple other options worth noting:

    • J# is Microsoft's Java language implementation on .NET. You can access Java libraries (up to version 1.4*, anyways). *actually Java 1.1.4 for java.io/lang, and 1.2 for java.util + keep in mind that J# end of life is ~ 2015-2017 for J# 2.0 redist

    • Mono's IKVM also runs Java on the CLR, with access to other .NET programs.

    • Microsoft Visual Studio 2005 comes with a "Java language conversion assistant" that converts Java programs to C# programs automatically for you.

    0 讨论(0)
提交回复
热议问题