我在哪里可以找到 Java 到 C# 的转换器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/443010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:32:55  来源:igfitidea点击:

Where can I find a Java to C# converter?

c#java

提问by CSharpDevLondon

I needed to convert a Java 1.5se app to C# 2.0.

我需要将 Java 1.5se 应用程序转换为 C# 2.0。

Does anyone know of a tool (preferably free/open source) to do this?

有谁知道一个工具(最好是免费/开源)来做到这一点?

采纳答案by Rasmus Faber

Java Language Conversion Assistant. Optionally installed with (at least) Visual Studio 2005 Standard Edition.

Java 语言转换助手。可选择安装(至少)Visual Studio 2005 标准版。

Select File/Open/Convert/Java Language Conversion Assistant.

选择文件/打开/转换/Java 语言转换助手。

Remember to manually go over the code afterwards. It will have lots of issues.

记得之后手动检查代码。会有很多问题。

回答by Jon Skeet

Even if there is such a tool, I'd highly recommend you to do the conversion by hand. Automatic converters will often faithfully reproduce the code, but ignore idioms - because they'd be really, really hard to get right.

即使有这样的工具,我也强烈建议您手动进行转换。自动转换器通常会忠实地重现代码,但会忽略习惯用法 - 因为它们真的很难做到正确。

Furthermore, the differences between generics in .NET and Java could lead to some very different decisions in the two codebases.

此外,.NET 和 Java 中泛型之间的差异可能会导致两个代码库中的一些非常不同的决定。

Really, you'll be better off doing it by hand.

真的,你最好手动完成。

回答by revs

Microsoft used to have their own Java to C# Converter - Microsoft Java Language Conversion Assistant 3.0

微软曾经有自己的 Java to C# Converter - Microsoft Java Language Conversion Assistant 3.0

回答by revs

ikvmexposes java classes in .NET. Its not a converter, but based on my experience I'd recommend it to anyone making the transition from java to .NET

ikvm在 .NET 中公开 Java 类。它不是转换器,但根据我的经验,我会向任何从 Java 过渡到 .NET 的人推荐它

回答by Carlo V. Dango

While the syntax of the two languages seems alike, the semantics are far different. To mention a few places the two languages go astray

虽然这两种语言的语法看起来很相似,但语义却大不相同。提一下这两种语言误入歧途的几个地方

  • Generics, Java compiles everything to objects, C# retains the generic types
  • Exceptions, Java has checked exceptions, C# doesn't
  • Anonymous classes and inner classes, Java has anonymous classes and nested classes, C# has neither. Instead C# has delegates and events. The programming model is thus very different
  • delegates, C# has the notion of function pointers which leads to a different way of programming.
  • events, C# has the notion of events and components which leads to a different way of programming.
  • API, setting all the semantical differences aside, both langauges has acompanied huge API's, neither of which are trivially converted.
  • 泛型,Java 将一切编译为对象,C# 保留泛型类型
  • 异常,Java 有检查异常,C# 没有
  • 匿名类和内部类,Java 有匿名类和嵌套类,C# 两者都没有。相反,C# 具有委托和事件。编程模型因此非常不同
  • 委托,C# 有函数指针的概念,这导致了不同的编程方式。
  • 事件,C# 有事件和组件的概念,这导致了不同的编程方式。
  • API,将所有语义差异放在一边,两种语言都有大量的 API,它们都没有被简单地转换。

In other words, you won't be able to make such a transition automatically. If the reason for changing to C# is to be able to translate your code into an .exe file, there are various options also in the Java market.

换句话说,您将无法自动进行此类转换。如果更改为 C# 的原因是为了能够将您的代码转换为 .exe 文件,那么 Java 市场上也有多种选择。

回答by alfdev

A good tool to use is called Sharpen, which is open source.

一个很好用的工具叫做 Sharpen,它是开源的。

This tool has been forked and updated by the Xamarin team and they used it to translate the Android APIs into C#. Their copy can be found here:

该工具已由 Xamarin 团队分叉和更新,他们使用它来将 Android API 转换为 C#。他们的副本可以在这里找到:

回答by magallanes

Right now, i am testing Tangible Solution (that is not for free)

现在,我正在测试有形解决方案(这不是免费的)

Tangible Source Converter

有形源转换器

And it add a Class Helper. But outside it,the result looks fine.

它添加了一个类助手。但在它之外,结果看起来不错。

Java: (original)

爪哇:(原文)

public class PackVariableArrays {


private ClassError myerror=new ClassError();


public VariableArrays unpack(String txt) {
    VariableArrays pc = new VariableArrays();
    Variable lc;
    txt=txt.replace("\r\n","\n");
    setMyerror(new ClassError());
    if (txt==null) {
        lc=new Variable();
        lc.name="ERV-5: Empty values";
        pc.addItem(lc);

        return pc;
    }
    String[] linecode = txt.split(ClassUtil.SEPARATOR2);
    int blen = 0;
    int tmpint = 0;

    int numelement = 9999;

C# (conversion)

C#(转换)

public class PackVariableArrays {


    private ClassError myerror =new ClassError();


    public virtual VariableArrays unpack(string txt) {
        VariableArrays pc = new VariableArrays();
        Variable lc;
        txt=txt.Replace("\r\n","\n");
        Myerror = new ClassError();
        if (txt==null) {
            lc=new Variable();
            lc.name="ERV-5: Empty values";
            pc.addItem(lc);

            return pc;
        }
        string[] linecode = StringHelperClass.StringSplit(txt, ClassUtil.SEPARATOR2, true);
        int blen = 0;
        int tmpint = 0;

        int numelement = 9999;

It is a easy case but it works fine. However, as i said early, it uses a Class Helper ( StringHelperClass.StringSplit) that is fine but it is unneeding. Outside it, the result code is pretty clear.

这是一个简单的案例,但效果很好。然而,正如我之前所说,它使用了一个很好但不需要的类助手(StringHelperClass.StringSplit)。在它之外,结果代码非常清楚。

回答by Panayotis

Have you tried XMLVM? It has an option to automatically convert to C# like this:

你试过XMLVM吗?它有一个选项可以像这样自动转换为 C#:

xmlvm --in=myjar.jar --out=output_dir --target=csharp

回答by Alex

Recently Xamarin have ported android to mono using sharpen. Check out thislink.

最近 Xamarin 使用锐化将 android 移植到了单声道。看看这个链接。