C#对象数组 对应js与ts之间的转换

C# 代码:

public class Program
{
    public static void Main()
    {
        A a1 = new A();
        A a2 = new A();
        A a3 = new A();
        A[] axs1 = new A[]{a1, a2, a3};
        A[] axs2 = new A[2]{a1, a3};
        A[] axs3 = new A[3];
        axs3[0] = a1;
        Console.WriteLine(axs1[0].a1 + "," + axs2[1].a2);
    }
}

public class A
{
    public int a1 = 312;
    public String a2 = "555";
    public void say(){
        var msg = "Hello, World!";
        Console.WriteLine(msg);
    }
}

JavaScript代码:

Bridge.assembly("Demo", function ($asm, globals) {
    "use strict";

    Bridge.define("Demo.A", {
        fields: {
            a1: 0,
            a2: null
        },
        ctors: {
            init: function () {
                this.a1 = 312;
                this.a2 = "555";
            }
        },
        methods: {
            say: function () {
                var msg = "Hello, World!";
                System.Console.WriteLine(msg);
            }
        }
    });

    Bridge.define("Demo.Program", {
        main: function Main () {
            var a1 = new Demo.A();
            var a2 = new Demo.A();
            var a3 = new Demo.A();
            var axs1 = System.Array.init([a1, a2, a3], Demo.A);
            var axs2 = System.Array.init([a1, a3], Demo.A);
            var axs3 = System.Array.init(3, null, Demo.A);
            axs3[System.Array.index(0, axs3)] = a1;
            System.Console.WriteLine(axs1[System.Array.index(0, axs1)].a1 + "," + (axs2[System.Array.index(1, axs2)].a2 || ""));
        }
    });
});

TypeScript代码:

declare module Demo {
    export interface A {
        a1: number;
        a2: string;
        say(): void;
    }
    export interface AFunc extends Function {
        prototype: A;
        new (): A;
    }
    var A: AFunc;

    export interface Program {
    }
    export interface ProgramFunc extends Function {
        prototype: Program;
        new (): Program;
        Main(): void;
    }
    var Program: ProgramFunc;
}


最后输出的结果: 312,555


C# 代码:

public class Program
{
    public static void Main()
    {
		BBKVector2d[] battlePoint = new BBKVector2d[10];
        battlePoint[0] = BBKVector2d.Zero; 
        battlePoint[1] = BBKVector2d.One;    
		for(int i = 2; i < 9; i++){
            battlePoint[i] = new BBKVector2d();
        }
        battlePoint[9] = BBKVector2d.Max;
    }
}

  public struct BBKVector2d
    {
        public double x;
        public double y;

        public const double kEpsilon = 1E-5d;
        public const double equalNum = 9.999999E-11d;

        public static BBKVector2d Zero
        {
            get
            {
                return new BBKVector2d(0, 0);
            }
        }

        public static BBKVector2d One
        {
            get
            {
                return new BBKVector2d(1, 1);
            }
        }

        public static BBKVector2d Max
        {
            get
            {
                return new BBKVector2d(double.MaxValue, double.MaxValue);
            }
        }

        public static BBKVector2d UnitX
        {
            get
            {
                return new BBKVector2d(1, 0);
            }
        }

        public static BBKVector2d UnitY
        {
            get
            {
                return new BBKVector2d(0, 1);
            }
        }

        public double Magnitude
        {
            get
            {
                return Math.Sqrt(x * x + y * y);
            }
        }

        public double SqrMagnitude
        {
            get
            {
                return x * x + y * y;
            }
        }


        public BBKVector2d(double x, double y)
        {
            this.x = x;
            this.y = y;
        }

        public BBKVector2d(BBKVector2d vec2)
        {
            this.x = vec2.x;
            this.y = vec2.y;
        }

        public BBKVector2d Normalized
        {
            get
            {
                BBKVector2d res = new BBKVector2d(this);
                double num = res.Magnitude;
                if (num <= 1e-05d)
                {
                    res.x = 0;
                    res.y = 0;
                }
                else
                {
                    res.x /= num;
                    res.y /= num;
                }

                return res;
            }
        }

        public BBKVector2d Inversed
        {
            get
            {
                return new BBKVector2d(-x, -y);
            }
        }

        public void Inverse()
        {
            x = -x;
            y = -y;
        }

        public void Normalize()
        {
            double num = Magnitude;
            if (num <= 1e-05d)
            {
                x = 0;
                y = 0;
            }
            else
            {
                x /= num;
                y /= num;
            }
        }

        public void Set(double _x, double _y)
        {
            x = _x;
            y = _y;
        }

        public void Add(BBKVector2d a)
        {
            x += a.x;
            y += a.y;
        }

        public void Add(double _x, double _y)
        {
            x += _x;
            y += _y;
        }

        public void Sub(BBKVector2d a)
        {
            x -= a.x;
            y -= a.y;
        }
        public void Sub(double _x, double _y)
        {
            x -= _x;
            y -= _y;
        }

        public void Mul(float _x, float _y)
        {
            x *= _x;
            y *= _y;
        }

        public void Mul(BBKVector2d v)
        {
            x *= v.x;
            y *= v.y;
        }

        public double Dot(BBKVector2d v)
        {
            return x * v.x + y * v.y;
        }

        public void MoveForward(double angle, double speed)
        {
            x += speed * Math.Cos(angle);
            y += speed * Math.Sin(angle);
        }

        public double Distance(BBKVector2d b)
        {
            return Math.Sqrt(DistanceSquared(b));
        }

        public double DistanceSquared(BBKVector2d b)
        {
            return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y);
        }

        public double Angle(BBKVector2d b)
        {
            return Math.Atan2(b.y - this.y, b.x - this.x) * 180f / Math.PI;
        }

        public override string ToString()
        {
            string dx = x.ToString("f4");
            string dy = y.ToString("f4");
            return string.Format("SimpleVector2({0}, {1})", dx, dy);
        }


        public static BBKVector2d Lerp(BBKVector2d from, BBKVector2d to, double t)
        {
            return new BBKVector2d(from.x + ((to.x - from.x) * t), from.y + ((to.y - from.y) * t));
        }


        public static double Distance(BBKVector2d a, BBKVector2d b)
        {
            return a.Distance(b);
        }

        public static double Angle(BBKVector2d from, BBKVector2d to)
        {
            return from.Angle(to);
        }

        public static BBKVector2d operator +(BBKVector2d a, BBKVector2d b)
        {
            return new BBKVector2d(a.x + b.x, a.y + b.y);
        }
        public static BBKVector2d operator +(BBKVector2d a, double[] b)
        {
            return new BBKVector2d(a.x + b[0], a.y + b[1]);
        }

        public static BBKVector2d operator -(BBKVector2d a, BBKVector2d b)
        {
            return new BBKVector2d(a.x - b.x, a.y - b.y);
        }
        public static BBKVector2d operator -(BBKVector2d a, double[] b)
        {
            return new BBKVector2d(a.x - b[0], a.y - b[1]);
        }

        public static BBKVector2d operator *(BBKVector2d a, int b)
        {
            return new BBKVector2d(a.x * b, a.y * b);
        }

        public static BBKVector2d operator *(BBKVector2d a, double b)
        {
            return new BBKVector2d(a.x * b, a.y * b);
        }

        public static BBKVector2d operator /(BBKVector2d a, int b)
        {
            return new BBKVector2d(a.x / b, a.y / b);
        }

        public static BBKVector2d operator /(BBKVector2d a, double b)
        {
            return new BBKVector2d(a.x / b, a.y / b);
        }

        public static bool operator ==(BBKVector2d lhs, BBKVector2d rhs)
        {
            return ((lhs - rhs).SqrMagnitude < equalNum);
        }

        public static bool operator !=(BBKVector2d lhs, BBKVector2d rhs)
        {
            return ((lhs - rhs).SqrMagnitude >= equalNum);
        }

        public override bool Equals(object obj)
        {
            return obj == null ? false : (x == ((BBKVector2d)obj).x && y == ((BBKVector2d)obj).y);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }

JavaScript代码:

Bridge.assembly("Demo", function ($asm, globals) {
    "use strict";

    Bridge.define("Demo.BBKVector2d", {
        $kind: "struct",
        statics: {
            fields: {
                kEpsilon: 0,
                equalNum: 0
            },
            props: {
                Zero: {
                    get: function () {
                        return new Demo.BBKVector2d.$ctor2(0, 0);
                    }
                },
                One: {
                    get: function () {
                        return new Demo.BBKVector2d.$ctor2(1, 1);
                    }
                },
                Max: {
                    get: function () {
                        return new Demo.BBKVector2d.$ctor2(System.Double.max, System.Double.max);
                    }
                },
                UnitX: {
                    get: function () {
                        return new Demo.BBKVector2d.$ctor2(1, 0);
                    }
                },
                UnitY: {
                    get: function () {
                        return new Demo.BBKVector2d.$ctor2(0, 1);
                    }
                }
            },
            ctors: {
                init: function () {
                    this.kEpsilon = 1E-05;
                    this.equalNum = 9.999999E-11;
                }
            },
            methods: {
                Lerp: function (from, to, t) {
                    return new Demo.BBKVector2d.$ctor2(from.x + ((to.x - from.x) * t), from.y + ((to.y - from.y) * t));
                },
                Distance: function (a, b) {
                    return a.Distance(b.$clone());
                },
                Angle: function (from, to) {
                    return from.Angle(to.$clone());
                },
                op_Addition: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x + b.x, a.y + b.y);
                },
                op_Addition$1: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x + b[System.Array.index(0, b)], a.y + b[System.Array.index(1, b)]);
                },
                op_Subtraction: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x - b.x, a.y - b.y);
                },
                op_Subtraction$1: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x - b[System.Array.index(0, b)], a.y - b[System.Array.index(1, b)]);
                },
                op_Multiply$1: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x * b, a.y * b);
                },
                op_Multiply: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x * b, a.y * b);
                },
                op_Division$1: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x / b, a.y / b);
                },
                op_Division: function (a, b) {
                    return new Demo.BBKVector2d.$ctor2(a.x / b, a.y / b);
                },
                op_Equality: function (lhs, rhs) {
                    return ((Demo.BBKVector2d.op_Subtraction(lhs.$clone(), rhs.$clone())).SqrMagnitude < Demo.BBKVector2d.equalNum);
                },
                op_Inequality: function (lhs, rhs) {
                    return ((Demo.BBKVector2d.op_Subtraction(lhs.$clone(), rhs.$clone())).SqrMagnitude >= Demo.BBKVector2d.equalNum);
                },
                getDefaultValue: function () { return new Demo.BBKVector2d(); }
            }
        },
        fields: {
            x: 0,
            y: 0
        },
        props: {
            Magnitude: {
                get: function () {
                    return Math.sqrt(this.x * this.x + this.y * this.y);
                }
            },
            SqrMagnitude: {
                get: function () {
                    return this.x * this.x + this.y * this.y;
                }
            },
            Normalized: {
                get: function () {
                    var res = new Demo.BBKVector2d.$ctor1(this);
                    var num = res.Magnitude;
                    if (num <= 1E-05) {
                        res.x = 0;
                        res.y = 0;
                    } else {
                        res.x /= num;
                        res.y /= num;
                    }

                    return res.$clone();
                }
            },
            Inversed: {
                get: function () {
                    return new Demo.BBKVector2d.$ctor2(-this.x, -this.y);
                }
            }
        },
        ctors: {
            $ctor2: function (x, y) {
                this.$initialize();
                this.x = x;
                this.y = y;
            },
            $ctor1: function (vec2) {
                this.$initialize();
                this.x = vec2.x;
                this.y = vec2.y;
            },
            ctor: function () {
                this.$initialize();
            }
        },
        methods: {
            Inverse: function () {
                this.x = -this.x;
                this.y = -this.y;
            },
            Normalize: function () {
                var num = this.Magnitude;
                if (num <= 1E-05) {
                    this.x = 0;
                    this.y = 0;
                } else {
                    this.x /= num;
                    this.y /= num;
                }
            },
            Set: function (_x, _y) {
                this.x = _x;
                this.y = _y;
            },
            Add: function (a) {
                this.x += a.x;
                this.y += a.y;
            },
            Add$1: function (_x, _y) {
                this.x += _x;
                this.y += _y;
            },
            Sub: function (a) {
                this.x -= a.x;
                this.y -= a.y;
            },
            Sub$1: function (_x, _y) {
                this.x -= _x;
                this.y -= _y;
            },
            Mul$1: function (_x, _y) {
                this.x *= _x;
                this.y *= _y;
            },
            Mul: function (v) {
                this.x *= v.x;
                this.y *= v.y;
            },
            Dot: function (v) {
                return this.x * v.x + this.y * v.y;
            },
            MoveForward: function (angle, speed) {
                this.x += speed * Math.cos(angle);
                this.y += speed * Math.sin(angle);
            },
            Distance: function (b) {
                return Math.sqrt(this.DistanceSquared(b.$clone()));
            },
            DistanceSquared: function (b) {
                return (this.x - b.x) * (this.x - b.x) + (this.y - b.y) * (this.y - b.y);
            },
            Angle: function (b) {
                return Math.atan2(b.y - this.y, b.x - this.x) * 180.0 / Math.PI;
            },
            toString: function () {
                var dx = System.Double.format(this.x, "f4");
                var dy = System.Double.format(this.y, "f4");
                return System.String.format("SimpleVector2({0}, {1})", dx, dy);
            },
            equals: function (obj) {
                return obj == null ? false : (this.x === System.Nullable.getValue(Bridge.cast(Bridge.unbox(obj), Demo.BBKVector2d)).x && this.y === System.Nullable.getValue(Bridge.cast(Bridge.unbox(obj), Demo.BBKVector2d)).y);
            },
            getHashCode: function () {
                return Bridge.getHashCode(this);
            },
            $clone: function (to) {
                var s = to || new Demo.BBKVector2d();
                s.x = this.x;
                s.y = this.y;
                return s;
            }
        }
    });

    Bridge.define("Demo.Program", {
        main: function Main () {
            var battlePoint = System.Array.init(10, function (){
                return new Demo.BBKVector2d();
            }, Demo.BBKVector2d);
            battlePoint[System.Array.index(0, battlePoint)] = Demo.BBKVector2d.Zero.$clone();
            battlePoint[System.Array.index(1, battlePoint)] = Demo.BBKVector2d.One.$clone();
            for (var i = 2; i < 9; i = (i + 1) | 0) {
                battlePoint[System.Array.index(i, battlePoint)] = new Demo.BBKVector2d.ctor();
            }
            battlePoint[System.Array.index(9, battlePoint)] = Demo.BBKVector2d.Max.$clone();
        }
    });
});

TypeScript 代码:

declare module Demo {
    export interface BBKVector2d {
        x: number;
        y: number;
        Magnitude: number;
        SqrMagnitude: number;
        Normalized: Demo.BBKVector2d;
        Inversed: Demo.BBKVector2d;
        Inverse(): void;
        Normalize(): void;
        Set(_x: number, _y: number): void;
        Add(a: Demo.BBKVector2d): void;
        Add$1(_x: number, _y: number): void;
        Sub(a: Demo.BBKVector2d): void;
        Sub$1(_x: number, _y: number): void;
        Mul$1(_x: number, _y: number): void;
        Mul(v: Demo.BBKVector2d): void;
        Dot(v: Demo.BBKVector2d): number;
        MoveForward(angle: number, speed: number): void;
        Distance(b: Demo.BBKVector2d): number;
        DistanceSquared(b: Demo.BBKVector2d): number;
        Angle(b: Demo.BBKVector2d): number;
        toString(): string;
        equals(obj: any): boolean;
        getHashCode(): number;
        $clone(to: Demo.BBKVector2d): Demo.BBKVector2d;
    }
    export interface BBKVector2dFunc extends Function {
        prototype: BBKVector2d;
        $ctor2: {
            new (x: number, y: number): BBKVector2d
        };
        $ctor1: {
            new (vec2: Demo.BBKVector2d): BBKVector2d
        };
        new (): BBKVector2d;
        ctor: {
            new (): BBKVector2d
        };
        kEpsilon: number;
        equalNum: number;
        Zero: Demo.BBKVector2d;
        One: Demo.BBKVector2d;
        Max: Demo.BBKVector2d;
        UnitX: Demo.BBKVector2d;
        UnitY: Demo.BBKVector2d;
        Lerp(from: Demo.BBKVector2d, to: Demo.BBKVector2d, t: number): Demo.BBKVector2d;
        Distance(a: Demo.BBKVector2d, b: Demo.BBKVector2d): number;
        Angle(from: Demo.BBKVector2d, to: Demo.BBKVector2d): number;
        op_Addition(a: Demo.BBKVector2d, b: Demo.BBKVector2d): Demo.BBKVector2d;
        op_Addition$1(a: Demo.BBKVector2d, b: number[]): Demo.BBKVector2d;
        op_Subtraction(a: Demo.BBKVector2d, b: Demo.BBKVector2d): Demo.BBKVector2d;
        op_Subtraction$1(a: Demo.BBKVector2d, b: number[]): Demo.BBKVector2d;
        op_Multiply$1(a: Demo.BBKVector2d, b: number): Demo.BBKVector2d;
        op_Multiply(a: Demo.BBKVector2d, b: number): Demo.BBKVector2d;
        op_Division$1(a: Demo.BBKVector2d, b: number): Demo.BBKVector2d;
        op_Division(a: Demo.BBKVector2d, b: number): Demo.BBKVector2d;
        op_Equality(lhs: Demo.BBKVector2d, rhs: Demo.BBKVector2d): boolean;
        op_Inequality(lhs: Demo.BBKVector2d, rhs: Demo.BBKVector2d): boolean;
    }
    var BBKVector2d: BBKVector2dFunc;

    export interface Program {
    }
    export interface ProgramFunc extends Function {
        prototype: Program;
        new (): Program;
        Main(): void;
    }
    var Program: ProgramFunc;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值