How to get declared type with mirrors?

import 'dart:mirrors';


void main() {
  var mirror = reflectClass(MyClass);
  mirror.declarations.forEach((k, v){
    print(k);
    if(v is VariableMirror){
      print(v.type);
    }
  });
}


class MyClass{
  var aDynamic;
  int anInt;
}

//prints:
Symbol("aDynamic")
TypeMirror on 'dynamic'
Symbol("anInt")
ClassMirror on 'int'
Symbol("MyClass")

is there a way that I can get a Type returning a TypeMirror so that Id would like to return the type dynamicand type intfor the two properties in the above example?

+3
source share
1 answer

VariableMirror.type- the right way. If it returns a ClassMirror, you can use ClassMirror.reflectedTypeto get the Type object for the declaration.

VariableMirror.type , , , , TypeMirror, ClassMirror. TypeVariableMirror TypedefMirror. , - TypeMirror dynamic. , . , , dynamic, . , , Object. TypedefMirrors Function. , , , .

:

final _dynamicType = reflectType(dynamic);

Type getDeclaredType(VariableMirror m) {
  var t = m.type;
  if (t is ClassMirror) return t.reflectedType;
  if (t is TypedefMirror) return Function;
  if (t == _dynamicType) return Object;
  throw new ArgumentError("I don't know how to handle ${t}");
}
+4

All Articles