首页 > 解决方案 > How can I keep the Generics type in the C# code output from Haxe?

问题描述

I'd like to use an Array with type in C#.

I tried building the following code in Haxe 4.0.5, but hoges is an Array<object> in C#. (I wanted Array<Hoge>)

class ArrayTest
{
    public var hoges: Array<Hoge>;
}

class Hoge
{
    public var x: Int;
    public var y: Int;
    public var z: Int;
}

I found the following post on github and understand that this behavior is a spec to make the code faster. https://github.com/HaxeFoundation/haxe/issues/5434#issuecomment-230581990.

However, I'm hoping it comes with a type because I want to use this code as an interface. Are there any workarounds?

标签: haxe

解决方案


如果主要是为了与外部代码交互,则使用特定于 C# 的集合可能更合适:

import cs.system.collections.generic.List_1;

class Main {
    public static var hoges:List_1<Hoge> = new List_1();
    static function main() {
        hoges.Add(new Hoge());
        trace(hoges[0]);
    }
}
class Hoge {
    public var x: Int;
    public var y: Int;
    public var z: Int;
    public function new() {}
}

产生

public static global::System.Collections.Generic.List<global::Hoge> hoges;

如您所料。

抽象可用于根据目标平台切换实现。


推荐阅读