简体   繁体   English

C#-> Java代码生成器

[英]C# --> Java code generator

I have a small (~2000 lines of code) class that I would like to use from both java & .NET. 我想在Java和.NET中使用一个小的(约2000行代码)类。 There are several approaches to do this - among them wrapping the class as a service, using some interop voodoo, or duplicating the code to both C# & java. 有几种方法可以做到这一点-其中包括使用互操作性伏都教将类包装为服务,或将代码复制到C#和Java。

Do you know an out-of-the-box tool that accomplishes the latter - takes a simple C# class with no dependencies and converts it to an equivalent java class? 您是否知道可以完成后者的现成工具-接受一个没有依赖项的简单C#类并将其转换为等效的Java类?

IKVM.NET在将jar文件并将其编译为托管的.NET程序集方面做得很好。

If it is small (or in fact, even if it is large), I'm not sure of the wisdom of mechanical translation tools; 如果它很小(或者实际上,即使它很大),我也不确定机械翻译工具的智慧。 I've simply never had much joy with them. 我从来没有对他们有太多的快乐。 However, one option would be to write the .NET code in J#. 但是,一种选择是用J#编写.NET代码。

But I stress - if it was me, I'd manually port it between the two manually and maintain them separately. 但我要强调-如果是我,我会手动将其手动移植到这两者之间,并分别进行维护。 Most of the time the differences aren't huge - signed bytes, the boxing differences, etc. Of course, anything with delegates will need changing - and captured variables work differently, etc etc. 大多数情况下,差异并不大-带符号的字节,装箱差异等。当然,具有委托的任何事物都需要更改-并且捕获的变量的工作方式不同,等等。

This is list of tools I know. 这是我知道的工具列表。 Sharpen or j2cstranslator looks like good options. Sharpen或j2cstranslator看起来是不错的选择。

曾经有一个COM桥,您可以通过regasm.exe或Visual Studio注册要在COM中使用的C#程序集。

It's not really what you asked for, but I would just create a simple C# to Java translator. 这并不是您真正要求的,但是我只是创建一个简单的C#到Java转换器。

The differences are not that huge and you seem to be the author of the source so you can avoid nasty constructs that are quite difficult to translate. 差异并不大,您似乎是原文的作者,因此可以避免很难翻译的讨厌结构。 That way your translator would be quite simple. 这样,您的翻译器将非常简单。 I would go from C# to Java because C# is more expressive, and you can emulate almost all the C# functions in Java. 我会从C#转到Java,因为C#更具表现力,并且您可以在Java中模拟几乎所有的C#函数。

Actually cs2java seems to do just that. 实际上, cs2java似乎就是这样做的。

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

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