Convertir C# en Java

Traduisez n'importe quelle bibliothèque ou application console C# de niveau entreprise vers son équivalent Java avec une prise en charge complète du .NET Framework. Traduisez des millions de lignes de code à la fois avec un minimum d'effort manuel.
Demander un service

CodePorting.Translator Cs2Java est une application de transpilage

Il peut être utilisé pour mettre en place une traduction automatique du code C# en amélioration continue vers Java afin de publier la même version du logiciel pour les deux langages sans modifier les API ou la documentation du code. Il peut également aider à fournir des applications et des bibliothèques C# sur des plates-formes sur lesquelles la prise en charge de .NET est absente ou problématique.

Convertissez sans effort le code C# grâce à nos fonctionnalités puissantes

CodePorting.Translator Cs2Java est une application de transpilation de code source à code source, permettant la reproduction des classes internes et des routines de la même manière qu'elles sont implémentées dans le code C# original.
public class A : B
{
    int B.this[int index]
    {
        get { return mArray[index]; }
        set { mArray[index] = value; }
    }

    private int[] mArray;
}

internal interface B
{
    int this[int index]
    {
        get; set;
    }
}


public class A implements B
{
    public final int get_Item(int index)
    {
        return mArray[index];
    }

    public final void set_Item(int index, int value)
    {
        mArray[index] = value;
    }

    private int[] mArray;
}

interface B
{
    int get_Item(int index);
    void set_Item(int index, int value);
}
using System.Threading;

class LambdaTest
{
    public void TestMPMM_1()
    {
        var a = 0;
        new Thread(() =>
        {
            var b = a;
        });
    }
}
import com.codeporting.ms.System.Threading.Thread;
import com.codeporting.ms.System.Threading.ThreadStart;

class LambdaTest
{
    private LambdaTest[] LambdaTest_this = {this};
    public final void testMPMM_1()
    {
        int[] a_closure = new int[]{0};
        new Thread(new ThreadStart() {
            public String getDelegateId() {
                return System.identityHashCode(LambdaTest_this[0]) + "-1342179863";
            }
            public void invoke() {
                int b = a_closure[0];
            }
        });
    }
}
using System;

class Program
{
    public static void Main(string[] args)
    {
        var printer = new Printer("\n");
        printer.Print("hello, Printer");
        IPrinter iprinter = printer;
        iprinter.Print("hello, IPrinter");
    }
}

interface IPrinter
{
    void Print(string text);
}

class Printer : IPrinter
{
    string endOfLine;

    public Printer(string endOfLine)
    {
        this.endOfLine = endOfLine;
    }

    public void Print(string text)
    {
        Console.Write($"[class method call] : {text}{endOfLine}");
    }

    void IPrinter.Print(string text)
    {
        Console.Write($"[interface method call] : {text}{endOfLine}");
    }
}
import com.codeporting.ms.System.Console;
import com.codeporting.ms.System.StringExtensions;

class Program
{
    public static void main(String[] args)
    {
        Printer printer = new Printer("\n");
        printer.print("hello, Printer");
        IPrinter iprinter = (printer).get_Printer_IPrinter();
        iprinter.print("hello, IPrinter");
    }
}

interface IPrinter
{
    void print(String text);
}

class Printer 
{
    private class Printer_IPrinter implements IPrinter
    {
        private Printer implementation;
        public Printer_IPrinter(Printer implementation)
        {
            this.implementation = implementation;
        }
        public final void print(String text)
        {
            Console.write(StringExtensions.format("[interface method call] : {0}{1}", text, this.implementation.endOfLine));
        }
    }

    final IPrinter get_Printer_IPrinter()
    {
        return new Printer_IPrinter(this);
    }

    private String endOfLine;

    public Printer(String endOfLine)
    {
        this.endOfLine = endOfLine;
    }

    public final void print(String text)
    {
        Console.write(StringExtensions.format("[class method call] : {0}{1}", text, endOfLine));
    }
}
using System;

class Vector2
{
    public float x, y;
    
    public Vector2(float x, float y)
    {
        this.x = x;
        this.y = y;
    }
    public Vector2()
    {
        this.x = 0f;
        this.y = 0f;
    }
    
    public override string ToString() => $"({x}, {y})";

    public static Vector2 operator +(Vector2 a, Vector2 b) => new Vector2(a.x + b.x, a.y + b.y);
    public static Vector2 operator -(Vector2 a, Vector2 b) => new Vector2(a.x - b.x, a.y - b.y);
    public static Vector2 operator *(Vector2 a, Vector2 b) => new Vector2(a.x * b.x, a.y * b.y);
    public static Vector2 operator /(Vector2 a, Vector2 b) => new Vector2(a.x / b.x, a.y / b.y);
    public static Vector2 operator *(Vector2 a, float b) => new Vector2(a.x * b, a.y * b);
    public static Vector2 operator /(Vector2 a, float b) => new Vector2(a.x / b, a.y / b);
}

class Program
{
    public static void Main(string[] args)
    {
        Vector2 a = new Vector2(1f, 2f);
        Vector2 b = new Vector2(3f, 4f);
        Vector2 c = (a + b) * 2f;
        Vector2 d = c / 10f;
        Console.WriteLine($"a = {a}, b = {b}, c = {c}, d = {d}");
    }
}
import com.codeporting.ms.System.Console;
import com.codeporting.ms.System.StringExtensions;

class Vector2
{
    public float x, y;
    
    public Vector2(float x, float y)
    {
        this.x = x;
        this.y = y;
    }
    public Vector2()
    {
        this.x = 0f;
        this.y = 0f;
    }
    @Override
    public /*override*/ String toString()
    {
        return StringExtensions.format("({0}, {1})", x, y);
    }
    public static Vector2 op_Addition(Vector2 a, Vector2 b)
    {
        return new Vector2(a.x + b.x, a.y + b.y);
    }
    public static Vector2 op_Subtraction(Vector2 a, Vector2 b)
    {
        return new Vector2(a.x - b.x, a.y - b.y);
    }
    public static Vector2 op_Multiply(Vector2 a, Vector2 b)
    {
        return new Vector2(a.x * b.x, a.y * b.y);
    }
    public static Vector2 op_Division(Vector2 a, Vector2 b)
    {
        return new Vector2(a.x / b.x, a.y / b.y);
    }
    public static Vector2 op_Multiply(Vector2 a, float b)
    {
        return new Vector2(a.x * b, a.y * b);
    }
    public static Vector2 op_Division(Vector2 a, float b)
    {
        return new Vector2(a.x / b, a.y / b);
    }
}

class Program
{
    public static void main(String[] args)
    {
        Vector2 a = new Vector2(1f, 2f);
        Vector2 b = new Vector2(3f, 4f);
        Vector2 c = Vector2.op_Multiply((Vector2.op_Addition(a, b)), 2f);
        Vector2 d = Vector2.op_Division(c, 10f);
        Console.writeLine(StringExtensions.format("a = {0}, b = {1}, c = {2}, d = {3}", a, b, c, d));
    }
}
using System;
using System.Collections;

public class Test
{
    public void PrintFibonacci()
    {
        Console.WriteLine("Fibonacci numbers:");

        foreach (int number in GetFibonacci(5))
        {
            Console.WriteLine(number);
        }
    }

    IEnumerable GetFibonacci(int maxValue)
    {
        int previous = 0;
        int current = 1;

        while (current <= maxValue)
        {
            yield return current;

            int newCurrent = previous + current;
            previous = current;
            current = newCurrent;
        }
    }
}
import com.codeporting.ms.lang.Operators;
import com.codeporting.ms.System.Collections.Generic.IGenericEnumerable;
import com.codeporting.ms.System.Collections.Generic.IGenericEnumerator;
import com.codeporting.ms.System.Collections.IEnumerable;
import com.codeporting.ms.System.Console;
import com.codeporting.ms.System.Environment;
import com.codeporting.ms.System.IDisposable;
import java.util.Iterator;

public class Test
{
    public final void printFibonacci()
    {
        Console.writeLine("Fibonacci numbers:");
        //Foreach to while statement conversion
        Iterator variable1 = getFibonacci(5).iterator();
        try
        {
            while (variable1.hasNext())
            {
                int number = Operators.unboxing(variable1.next(),int.class);
                Console.writeLine(number);
            }
        }
        finally
        {
            IDisposable variable2 = Operators.as(variable1, IDisposable.class);
            if (variable2 != null)
            {
                variable2.dispose();
            }
        }
    }

    private IEnumerable getFibonacci(int maxValue)
    {
        return new YieldIterator_GetFibonacci(-2, this, maxValue);
    }

    private class YieldIterator_GetFibonacci implements IGenericEnumerable<Object>, IGenericEnumerator<Object>, IDisposable
    {
        private int __state;
        private Object __current;
        private long __initialThreadId;
        private Test __this;
        private int previous;
        private int current;
        private int maxValue;
        public YieldIterator_GetFibonacci(int __state, Test __this, int maxValue)
        {
            this.__state = __state;
            this.__this = __this;
            this.maxValue = maxValue;
            __initialThreadId = Thread.currentThread().getId();
        }

        public final void dispose()
        {
        }

        public final boolean hasNext()
        {
            switch (__state)
            {
                case 0:
                    previous = 0;
                    current = 1;
                    if (current <= maxValue)
                    {
                        __current = current;
                        __state = 1;
                        return true;
                    }
                    else
                    {
                        __state = 2;
                        return hasNext();
                    }

                case 1:
                    int newCurrent = previous + current;
                    previous = current;
                    current = newCurrent;
                    if (current <= maxValue)
                    {
                        __current = current;
                        __state = 1;
                        return true;
                    }
                    else
                    {
                        __state = 2;
                        return hasNext();
                    }

                default:
                    __state = -1;
                    return false;
            }
        }

        public final Object next()
        {
            return __current;
        }

        public final void reset()
        {
            throw new com.codeporting.ms.System.NotSupportedException();
        }

        public final IGenericEnumerator<Object> iterator()
        {
            if (__state == -2 && __initialThreadId == Thread.currentThread().getId())
            {
                __state = 0;
                return this;
            }
            else
            {
                return new YieldIterator_GetFibonacci(0, __this, maxValue);
            }
        }
    }
}

csharp converter feature
Dépendances externes
Remplacez le code écrit manuellement en remplacement de la dépendance qui n'est pas disponible pour la traduction
csharp converter feature
Préservation des API
Les constructions spécifiques au langage utilisées dans le code C# sont traduites en équivalents Java les mieux adaptés

CodePorting.Translator Java Class Library

Pour convertir un projet C# en Java, il ne suffit pas de traduire uniquement le code source d'un langage à un autre. Évidemment, pour exécuter du code C#, il faut la bibliothèque de classes .NET Framework. Par conséquent, pour exécuter le code Java traduit, il faudrait également quelque chose de similaire.

La CodePorting.Translator Java Class Library offre un remplacement Java de la bibliothèque de classes du .NET Framework, en conservant la logique et la structure de la bibliothèque de classes du .NET Framework, ce qui permet au projet traduit de se sentir comme chez lui, en le masquant de l'implémentation de la plateforme Java. En plus des fonctionnalités de base de .NET, notre bibliothèque prend en charge les sous-systèmes System.Net, System.Drawing, System.XML, System.Security, et d'autres.
csharp to java conversion scheme

Exemples de produits C# traduits avec succès en Java

Articles liés :