platform/experiment/container/example1/bin/main.dart

45 lines
1.1 KiB
Dart
Raw Normal View History

2023-01-29 12:26:35 +00:00
import 'dart:mirrors';
2023-01-29 14:54:00 +00:00
import 'package:example1/src/models.dart';
2023-01-29 12:26:35 +00:00
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);
2023-01-29 14:54:00 +00:00
printAnnotationValue(Square);
2023-01-29 12:26:35 +00:00
}
class Shape {
void draw() => print("Draw Shape");
}
2023-01-29 14:54:00 +00:00
@Person('Will', 'Tom')
class Square {
void greetHii() {
print("Hii Welcome to flutter agency");
}
}
2023-01-29 12:26:35 +00:00
void printAnnotationValue(final Type clazz) {
final DeclarationMirror clazzDeclaration = reflectClass(clazz);
2023-01-29 14:54:00 +00:00
final ClassMirror someAnnotationMirror = reflectClass(Person);
2023-01-29 12:26:35 +00:00
final annotationInstsanceMirror =
clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror);
if (annotationInstsanceMirror.isEmpty) {
2023-01-29 16:04:15 +00:00
print('No annotated class found');
2023-01-29 12:26:35 +00:00
return;
}
final someAnnotationInstance =
2023-01-29 14:54:00 +00:00
(annotationInstsanceMirror.first.reflectee as Person);
print(someAnnotationInstance.firstName);
2023-01-29 12:26:35 +00:00
}