将 C# 转换为 Java

将任何企业级的C#库或控制台应用程序转换为具有完整.NET框架支持的Java等价物。一次性转换数百万行代码,只需最少的人工努力。
请求服务

CodePorting.Translator Cs2Java 是一款转译应用程序

它可以用于设置自动转换不断改进的C#代码到Java,以便为两种语言发布相同版本的软件,而不改变API或代码文档。它还可以帮助将C#应用程序和库交付到.NET支持缺失或有问题的平台。

利用我们强大的功能轻松转换 C# 代码

CodePorting.Translator Cs2Java 是一款从源代码到源代码的转译应用程序,允许以与 C# 原始代码中相同的方式复制内部类和例程。
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
外部依赖
用手动编写的代码替代无法翻译的依赖项
csharp converter feature
API 保存
C# 代码中使用的特定于语言的构造被转换为最匹配的 Java 等效项

CodePorting.Translator Java Class Library

要将 C# 项目转换为 Java,仅仅将源代码从一种语言转换为另一种语言是不够的。显然,执行 C# 代码需要 .NET Framework 类库。因此,要执行翻译好的 Java 代码,也需要类似的东西。

CodePorting.Translator Java Class Library提供了.NET Framework类库的Java替代品,保留了.NET Framework类库的逻辑和结构,这使得翻译的项目感觉宾至如归,将其隐藏在Java平台实现中。 除了 .NET 核心功能之外,我们的库还支持 System.Net、System.Drawing、System.XML、System.Security 和其他子系统。
csharp to java conversion scheme

成功翻译为 Java 的 C# 产品示例

相关文章