简体   繁体   English

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

[英]How to rewrite or convert C# code in Java code?

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. 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.

Is there any good way to translate C# code in Java?有什么好的方法可以翻译 Java 中的 C# 代码吗? The server application used no .net specific feature, only cross language tools like Spring.net, Hibernate.net and log4net.服务器应用程序没有使用 .net 特定功能,仅使用 Spring.net、Hibernate.net 和 log4net 等跨语言工具。

Thanks.谢谢。

I'd suggest building for Mono.我建议为 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 .但是,如果您想为 Java 构建,您可以查看Grasshopper It's a commercial product, but it claims to be able to translate CIL (the output of the C# compiler) to Java bytecodes.这是一个商业产品,但它声称能够将 CIL(C# 编译器的 output)转换为 Java 字节码。

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.除了可能的解决方案之外,将用一种语言编写的程序直接翻译成另一种语言通常被认为是 Bad Idea™——尤其是如果这种翻译是以某种自动化方式完成的。 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 中重写它可能符合您的最大利益以及必须维护此应用程序的人。

I only know the other way.我只知道另一种方式。 Dbo4 is developed in java and the c# version is generated from the java sources automaticaly. Dbo4是在 java 中开发的,c# 版本是从 java 源自动生成的。

There is no good way.没有什么好办法。 My recommendation is to start over in Java, or like you said use Mono.我的建议是从 Java 重新开始,或者像你说的那样使用 Mono。

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.我认为 Mono 方式会更好。 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#.必须重写代码只会增加项目的成本,特别是如果您已经有大量用 C# 编写的代码。 I, personally, try to avoid rewriting code whenever possible.我个人尽量避免重写代码。

Java and C# are pretty close in syntax and semantics. Java 和 C# 在语法和语义上非常接近。 The real problem is the little differences.真正的问题是细微的差异。 They will bite you when you dont expect it.当你没想到时,它们会咬你。

Grasshopper is really the best solution at this time, if the licensing works for you (the free version has some significant limitations).如果许可适合您,Grasshopper 确实是目前最好的解决方案(免费版本有一些重大限制)。 Its completely based on the Mono class libs (which are actually pretty good), but runs on top of standard Java VMs.它完全基于 Mono class 库(实际上非常好),但在标准 Java VM 之上运行。 Thats good as the Java VMs are generally a bit faster and more stable than Mono, in my experience.根据我的经验,Java VM 通常比 Mono 更快、更稳定。 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.在表单/图形相关的 API 方面,它确实比 Mono 有更多的弱点,因为其中大部分还没有从 Z5D9B47BD3B65072E0D5DAF55F01DA086 移植到 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 虚拟机上运行相同代码时还要好。 :) :)

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.你不想在以后生成代码,但是维护起来很麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM