haxe - 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?
解决方案
如果主要是为了与外部代码交互,则使用特定于 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;
如您所料。
抽象可用于根据目标平台切换实现。
推荐阅读
- haskell - 如何在 Haskell 中删除字符串中的最后一个字符(不使用 init)?
- pandas - 在 Pandas 中重命名我的数据框的列名
- python - 构建用户旅程 - 你如何“自我循环”加入?
- flutter - Lottie 小部件没有动画,因为 parent 是有状态的小部件
- python - 有条件地创建一个等于熊猫单元格中的值的列
- python - 分析字符串输入,直到它到达python中的某个元素
- c# - 在 C# 中具有相同类名的不同 HTML 节点
- node.js - Mongoose updateMany() 在嵌套模型中不起作用
- android - 如何在应用程序之外复制 Google Pay 流程?
- javascript - 颜色特定文本