views:

367

answers:

4

I have a small (~2000 lines of code) class that I would like to use from both java & .NET. 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.

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?

+2  A: 

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

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.

Marc Gravell
We write code in subset of J# and Java and it's kind of ugly, but works. Mind you, J# is no longer supported (or is in extended suppord, don't remember). For instance, Visual Studio 2008 don't have intellisence for it.
ya23
+3  A: 

IKVM.NET does pretty good job in taking a jar file and compiling it to a managed .NET assembly.

Darin Dimitrov
A: 

There used to be a COM bridge and you can register C# assemblies for use in COM with regasm.exe or visual studio.

Brian Hedler
A: 

It's not really what you asked for, but I would just create a simple C# to Java translator.

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.

Actually cs2java seems to do just that.

Steve Schnepp
It isn't really that simple as to write a translator even though they are similar in syntax. There are some features missing in Java such as delegates/events, lambda functions and extension methods plus there are a lot of discrepancies between the frameworks. Translating all of that isn't trivial.
Spoike
Wrong - Delegates and may of the extras (properties, events etc) in .net are just syntactical sugar for something that takes more keystrokes in java. Behind the scenes the compiler most of time synthetically generates a type etc to get the job done.
mP