【发布时间】:2013-11-26 03:16:04
【问题描述】:
如果有Type,使用Mirrors可以得到Type的名字。反过来,给定Type 的名字,你如何得到Type?
例如,来自以Dart 为中心的Angular 版本:
index.html
<form ng-controller='word?reset=true' >
...
</form>
mylib.dart
class Controller {
Controller( Brando brando, Element elem, Map args ) { ... }
}
class Word extends Controller { ... }
class LangList extends Controller { ... }
// Brando, the godfather
class Brando {
...
void compile( Element el ) {
...
// add controller
if( el.attributes.contains( 'ng-controller' ) {
var name = el.attributes.getTypeName(); <== "Word"
var args = el.attributes.getTypeArgs(); <== { 'reset': 'true' }
var type = <get type from camelized Type name> <=== how??
this.controllers.add( reflectClass(type).newInstance(
const Symbol(''), [this,el,args]).reflectee ); <=== instance from type
}
...
}
}
知道如何获取Type 的名称,如何从class 和Object 中获取Type,并知道如何实例化Type。缺少最后一块 - 你如何从它的名字中推导出 Type?
【问题讨论】:
标签: dart dart-mirrors