A TypeMirror has a member hasReflectedType and a member reflectedType, and the latter returns a Type for the type which is mirrored by this TypeMirror.
Here is an example of the kind of data provided by reflectedType:
<Type>[
prefix0.A,
const r.FakeType(r'test_reflectable.test.class_property_test._B'),
prefix0.D,
prefix0.C
]
In many cases, these Type objects are obtained by evaluating a typename denoting a class or type alias (for instance, the library imported with prefix prefix0 above contains class A, class C, class D).
We could actually create a construct that would enable the use of such types as types:
<void Function(void Function<X>())>[
(f) => f<prefix0.A>(),
(f) => throw "Cannot `callWithReflectedType` when type is private",
(f) => f<prefix0.D>(),
(f) => f<prefix0.C>(),
];
The TypeMirror class would have a new member callWithReflectedType:
abstract class TypeMirror implements DeclarationMirror {
...
void callWithReflectedType(void Function<X>() f) =>
reflector.typeCallers[reflectedTypeIndex](f);
...
}
We would then be able to use it as follows:
@reflector
class C {}
void main() {
TypeMirror typeMirror = reflector.reflectType(C);
late Map map;
if (typeMirror.hasReflectedType) { // This will be true, so we ignore the `else` case.
typeMirror.callWithReflectedType(<X>() => map = <String, X>{});
}
}
The callback which is passed to callWithReflectedType will receive the actual type mirrored by the TypeMirror as a normal Dart type argument, which means that we can use that type under the name X (or whatever we choose to call the type variable of the callback). For instance, we can create a Map<String, C> when the type mirror is a mirror of C.
A
TypeMirrorhas a memberhasReflectedTypeand a memberreflectedType, and the latter returns aTypefor the type which is mirrored by thisTypeMirror.Here is an example of the kind of data provided by
reflectedType:In many cases, these
Typeobjects are obtained by evaluating a typename denoting a class or type alias (for instance, the library imported with prefixprefix0above containsclass A,class C,class D).We could actually create a construct that would enable the use of such types as types:
The
TypeMirrorclass would have a new membercallWithReflectedType:We would then be able to use it as follows:
The callback which is passed to
callWithReflectedTypewill receive the actual type mirrored by theTypeMirroras a normal Dart type argument, which means that we can use that type under the nameX(or whatever we choose to call the type variable of the callback). For instance, we can create aMap<String, C>when the type mirror is a mirror ofC.