如何在 Java 代码中重写或转换 C# 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/65058/
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-03 10:30:26  来源:igfitidea点击:

How to rewrite or convert C# code in Java code?

提问by Seb

I start to write a client - server application using .net (C#) for both client and server side.

我开始使用 .net (C#) 为客户端和服务器端编写客户端 - 服务器应用程序。

Unfortunately, my company refuse to pay for Windows licence on server box meaning that I need to rewrite my code in Java, or go to the Mono way.

不幸的是,我的公司拒绝为服务器盒上的 Windows 许可证付费,这意味着我需要用 Java 重写我的代码,或者使用 Mono 方式。

Is there any good way to translate C# code in Java ? The server application used no .net specific feature, only cross language tools like Spring.net, Hibernate.net and log4net.

有什么好的方法可以在 Java 中翻译 C# 代码吗?服务器应用程序没有使用 .net 特定功能,仅使用跨语言工具,如 Spring.net、Hibernate.net 和 log4net。

Thanks.

谢谢。

采纳答案by Nate Kohari

I'd suggest building for Mono. You'll run into some gray area, but overall it's great. However, if you want to build for Java, you might check out Grasshopper. It's a commercial product, but it claims to be able to translate CIL (the output of the C# compiler) to Java bytecodes.

我建议为 Mono 构建。你会遇到一些灰色地带,但总体来说很棒。但是,如果您想为 Java 构建,您可以查看Grasshopper。这是一个商业产品,但它声称能够将 CIL(C# 编译器的输出)转换为 Java 字节码。

回答by ordnungswidrig

I only know the other way. Dbo4is developed in java and the c# version is generated from the java sources automaticaly.

我只知道另一种方式。Dbo4是用 java 开发的,c# 版本是从 java 源自动生成的。

回答by Nick Berardi

There is no good way. My recommendation is to start over in Java, or like you said use Mono.

没有什么好办法。我的建议是用 Java 重新开始,或者像你说的那样使用 Mono。

回答by Thomas Owens

Although I think the first mistake was choosing an implementation language without ensuring a suitable deployment environment, there's nothing that can be done about that now. I would think the Mono way would be better. Having to rewrite code would only increase the cost of the project, especially if you already have a good amount of code written in C#. I, personally, try to avoid rewriting code whenever possible.

虽然我认为第一个错误是在没有确保合适的部署环境的情况下选择了一种实现语言,但现在对此无能为力。我认为 Mono 方式会更好。不得不重写代码只会增加项目的成本,特别是如果您已经有大量用 C# 编写的代码。我个人尽量避免重写代码。

回答by Brian Phillips

Possible solutions aside, direct translations of programs written in one language to a different language is generally considered a Bad Idea™ -- especially if this translation is done in some automated fashion. Even when done by a "real" programmer, translating an application line by line often results in a less than desirable end result because each language has its own idioms, strengths and weaknesses that require things be done in a slightly different way.

撇开可能的解决方案不谈,将用一种语言编写的程序直接翻译成另一种语言通常被认为是一个坏主意™——特别是如果这种翻译是以某种自动方式完成的。即使由“真正的”程序员完成,逐行翻译应用程序通常会导致不太理想的最终结果,因为每种语言都有自己的习惯用法、优点和缺点,需要以略有不同的方式来完成。

As painful as it may be, it's probably in your best interest and those who have to maintain this application to rewrite it in Java if that's what your employer requires.

尽管它可能很痛苦,但如果您的雇主需要,这可能符合您的最大利益以及那些必须维护此应用程序以用 Java 重写它的人。

回答by EricSchaefer

Java and C# are pretty close in syntax and semantics. The real problem is the little differences. They will bite you when you dont expect it.

Java 和 C# 在语法和语义上非常接近。真正的问题是细微的差别。当你没想到的时候,他们会咬你。

回答by jsight

Grasshopper is really the best solution at this time, if the licensing works for you (the free version has some significant limitations). Its completely based on the Mono class libs (which are actually pretty good), but runs on top of standard Java VMs. Thats good as the Java VMs are generally a bit faster and more stable than Mono, in my experience. It does have more weaknesses than Mono when it comes to Forms/Graphics related APIs, as much of this hasn't been ported to Java from the Mono VM, however.

Grasshopper 确实是目前最好的解决方案,如果许可适用于您(免费版本有一些明显的限制)。它完全基于 Mono 类库(实际上非​​常好),但运行在标准 Java VM 之上。根据我的经验,这很好,因为 Java VM 通常比 Mono 更快、更稳定。在与表单/图形相关的 API 方面,它确实比 Mono 有更多的弱点,因为其中大部分还没有从 Mono VM 移植到 Java。

In the cases were it works, it can be wonderful, though. The performance is sometimes even better than when running the same code on MS's VM on Windows. :)

不过,在它有效的情况下,它可能会很棒。性能有时甚至比在 Windows 上的 MS 的 VM 上运行相同的代码时还要好。:)

回答by Alfonso Pajares

I would say from a maintance stand point rewrite the code. It's going to bring the initial cost of the projet up but would be less labor intensive later for whoever is looking at the code. Like previous posters stated anything automated like this can't do as good as a job as a "real" programmer and doing line by line converting won't help much either. You don't want to produce code later on that works but is hell to maintain.

我会说从维护的角度来看重写代码。这将增加项目的初始成本,但对于查看代码的人来说,以后的劳动强度会降低。就像之前的海报所说的那样,任何像这样自动化的东西都不能像“真正的”程序员那样做,并且逐行转换也无济于事。您不想在以后生成代码,但要维护却是地狱。