Added example
This commit is contained in:
parent
342a5a4ac9
commit
05cbd71248
3 changed files with 49 additions and 1 deletions
|
@ -35,7 +35,7 @@ void printAnnotationValue(final Type clazz) {
|
|||
final annotationInstsanceMirror =
|
||||
clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror);
|
||||
if (annotationInstsanceMirror.isEmpty) {
|
||||
print('Annotation is not on this class');
|
||||
print('No annotated class found');
|
||||
return;
|
||||
}
|
||||
final someAnnotationInstance =
|
||||
|
|
43
experiment/container/example2/bin/main.dart
Normal file
43
experiment/container/example2/bin/main.dart
Normal file
|
@ -0,0 +1,43 @@
|
|||
import 'package:example2/src/models.dart';
|
||||
|
||||
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);
|
||||
printAnnotationValue(Square);
|
||||
}
|
||||
|
||||
class Shape {
|
||||
void draw() => print("Draw Shape");
|
||||
}
|
||||
|
||||
@Person('Will', 'Tom')
|
||||
class Square {
|
||||
void greetHii() {
|
||||
print("Hii Welcome to flutter agency");
|
||||
}
|
||||
}
|
||||
|
||||
void printAnnotationValue(final Type clazz) {
|
||||
final DeclarationMirror clazzDeclaration = reflectClass(clazz);
|
||||
final ClassMirror someAnnotationMirror = reflectClass(Person);
|
||||
final annotationInstsanceMirror =
|
||||
clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror);
|
||||
if (annotationInstsanceMirror.isEmpty) {
|
||||
print('No annotated class found');
|
||||
return;
|
||||
}
|
||||
final someAnnotationInstance =
|
||||
(annotationInstsanceMirror.first.reflectee as Person);
|
||||
print(someAnnotationInstance.firstName);
|
||||
}
|
5
experiment/container/example2/lib/src/models.dart
Normal file
5
experiment/container/example2/lib/src/models.dart
Normal file
|
@ -0,0 +1,5 @@
|
|||
class Person {
|
||||
final String firstName;
|
||||
final String lastName;
|
||||
const Person(this.firstName, this.lastName);
|
||||
}
|
Loading…
Reference in a new issue