質問

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