如何在 Haxe 的 C# 代码输出中保留泛型类型?

How can I keep the Generics type in the C# code output from Haxe?

我想在 C# 中使用带有类型的数组。

我尝试在 Haxe 4.0.5 中构建以下代码,但 hoges 是 C# 中的 Array<object>。 (我想要Array<Hoge>

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

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

我在 github 上发现了以下 post 并了解此行为是使代码更快的规范。 https://github.com/HaxeFoundation/haxe/issues/5434#issuecomment-230581990.

但是,我希望它带有一个类型,因为我想将这段代码用作一个接口。 有什么解决方法吗?

如果主要是为了与外部代码交互,使用特定于 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;

如您所料。

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

您可以使用 NativeArray

typedef Hoges = cs.NativeArray<Hoge>;
class ArrayTest { public var hoges: Hoges; }

正在生成

public global::Hoge[] hoges;