문제

Given I have this class

class Animal {

    int age;

}

and that I have a String that contains "age".

How do I get the field type int from either a ClassMirror or an InstanceMirror?

도움이 되었습니까?

해결책

  // 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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top