鉴于我有这个课程
class Animal {
int age;
}
并且我有一个包含“age”的字符串。
如何从int
或ClassMirror
获取字段类型InstanceMirror
?
答案 0 :(得分:3)
// get ClassMirror
// either from instance
var a = new Animal();
InstanceMirror im = reflect(a);
ClassMirror cm = im.type;
// or from type
ClassMirror cm = reflectClass(Animal);
// get type info of the field from ClassMirror
VariableMirror vm = cm.declarations[#age]; // or cm.declaration[new Symbol('age')];
print(vm.type.qualifiedName);
print(vm.type.simpleName);