platform/experiment/container/example1/bin/main.dart
thomashii@dukefirehawk.com ec68a78c1d Test reflection
2023-01-29 20:26:35 +08:00

35 lines
957 B
Dart

import 'dart:mirrors';
void main() {
final stopwatch = Stopwatch()..start();
var reflectedClass = reflect(Shape());
reflectedClass.invoke(#draw, []);
//reflectedClass.invoke(Symbol('draw'), []);
print('Reflection executed in ${stopwatch.elapsed.inMilliseconds} ms');
stopwatch.stop();
printAnnotationValue(String);
printAnnotationValue(Shape);
}
class Shape {
void draw() => print("Draw Shape");
}
void printAnnotationValue(final Type clazz) {
final DeclarationMirror clazzDeclaration = reflectClass(clazz);
final ClassMirror someAnnotationMirror = reflectClass(Shape);
final annotationInstsanceMirror =
clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror);
if (annotationInstsanceMirror.isEmpty) {
print('Annotation is not on this class');
return;
}
final someAnnotationInstance =
(annotationInstsanceMirror.first.reflectee as Shape);
print("${someAnnotationInstance.draw}");
}