【问题标题】:Extract T from class<T> and use T in class building从 class<T> 中提取 T 并在类构建中使用 T
【发布时间】:2021-04-09 19:06:25
【问题描述】:

我想从Type.resolveClass() 得到一个Class&lt;T&gt;,并用它在一个类中构建一个T 类型的变量。我不太清楚如何描述它,所以这里有一些代码希望能很好地描述它。

public static function build(name:String) { 
var fields = Context.getBuildFields();
// Get Class<T>
var resolved = Type.resolveClass(name); 

// Do some magic to get a ComplexType that represents the T in Class<T>
var type = magic(resolved); // I want to find out how to do this

// Create a field of type T
fields.push({
  name: "ofTypeName",
  pos: Context.currentPos(),
  access: [AStatic, APublic],
  kind: FVar(type, macro null)
});


return fields;
}

所以我想知道如何从代表TClass&lt;T&gt; 中获取ComplexType

【问题讨论】:

  • Type.resolveClass()(运行时反射)在宏中似乎很不合适。有问题的类很有可能甚至没有在宏上下文中加载。

标签: haxe


【解决方案1】:
var type = Context.getType(name);
var ctype = TypeTools.toComplexType(type);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多