dart-mirrors

Find all subclasses in dart

你离开我真会死。 提交于 2019-11-29 07:05:26
I have three classes in dart: class A {} class B extends A{} class C extends A{} There is a way to get all subclasses from A? Edit: Thanks Alexandre Ardhuin your solution worked perfectly! I'm learning the dart i edited your code and put the recursive solution, take a look: import 'dart:mirrors'; class A {} class B extends A{} class C extends A{} class D {} class E extends C {} bool isParent(String parent,ClassMirror clazz){ var objectParentName = MirrorSystem.getName(clazz.superclass.simpleName); if (objectParentName == "Object"){ return parent == "Object"; } var result = parent ==

How to perform runtime type checking in Dart?

大兔子大兔子 提交于 2019-11-28 21:19:58
问题 Dart specification states: Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages). Sounds great, but there is no instanceof -like operator. So how do we perform runtime type-checking in Dart? Is it possible at all? 回答1: The instanceof-operator is called is in Dart. The spec isn't exactly friendly to a casual reader, so the best description right now

Create an instance of an object from a String in Dart?

懵懂的女人 提交于 2019-11-28 12:28:29
How would I do the Dart equivalent of this Java code? Class<?> c = Class.forName("mypackage.MyClass"); Constructor<?> cons = c.getConstructor(String.class); Object object = cons.newInstance("MyAttributeValue"); (From Jeff Gardner) The Dart code: ClassMirror c = reflectClass(MyClass); InstanceMirror im = c.newInstance(const Symbol(''), ['MyAttributeValue']); var o = im.reflectee; Learn more from this doc: http://www.dartlang.org/articles/reflection-with-mirrors/ (From Gilad Bracha) Using built_mirrors you can do it next way: library my_lib; import 'package:built_mirrors/built_mirrors.dart';

Dynamic class method invocation in Dart

大憨熊 提交于 2019-11-28 12:11:51
Like the question at Dynamic class method invocation in PHP I want to do this in Dart. var = "name"; page.${var} = value; page.save(); Is that possible? Kai Sellgren There are several things you can achieve with Mirrors . Here's an example how to set values of classes and how to call methods dynamically: import 'dart:mirrors'; class Page { var name; method() { print('called!'); } } void main() { var page = new Page(); var im = reflect(page); // Set values. im.setField("name", "some value").then((temp) => print(page.name)); // Call methods. im.invoke("method", []); } In case you wonder, im is

Executing bundle of functions by their metadata tag in Dart lang

↘锁芯ラ 提交于 2019-11-28 11:28:45
问题 Building on this, I want to write a code that run A:: the functions refeed by the same metadata tag. I tunes the codes of the previous thread as below: getFunctionMirrorsByTag.dart library impl; @MirrorsUsed(metaTargets: Tag) import 'dart:mirrors'; class Tag { final Symbol name; const Tag(this.name); } List<ClassMirror> getClassMirrorsByTag(Symbol name) { List res = new List(); MirrorSystem ms = currentMirrorSystem(); ms.libraries.forEach((u, lm) { lm.declarations.forEach((s, dm) { dm

How do I access metadata annotations from a class?

為{幸葍}努か 提交于 2019-11-28 07:07:06
问题 I have a Dart class that is annotated with metadata: class Awesome { final String msg; const Awesome(this.msg); String toString() => msg; } @Awesome('it works!') class Cool { } I want to see if Cool was annotated, and if so, with what. How do I do that? 回答1: Use the dart:mirrors library to access metadata annotations. import 'dart:mirrors'; class Awesome { final String msg; const Awesome(this.msg); String toString() => msg; } @Awesome('it works!') class Cool { } void main() { ClassMirror

Find all subclasses in dart

廉价感情. 提交于 2019-11-28 00:37:56
问题 I have three classes in dart: class A {} class B extends A{} class C extends A{} There is a way to get all subclasses from A? Edit: Thanks Alexandre Ardhuin your solution worked perfectly! I'm learning the dart i edited your code and put the recursive solution, take a look: import 'dart:mirrors'; class A {} class B extends A{} class C extends A{} class D {} class E extends C {} bool isParent(String parent,ClassMirror clazz){ var objectParentName = MirrorSystem.getName(clazz.superclass

Refer classes by their metadata tag

瘦欲@ 提交于 2019-11-27 16:27:37
Is it possible to find(probably with the mirror API) all classes(in my project) with some metadata annotation? Example: import 'baz.dart'; //more tagged classes @Tag(#foo) class A{ } @Tag(#foo) class B{ } void main() { List<ClassMirror> li = getClassMirrorsByTag(#foo); } I have found the answer: getClassMirrorsByTag.dart library impl; @MirrorsUsed(metaTargets: Tag) import 'dart:mirrors'; class Tag { final Symbol name; const Tag(this.name); } List<ClassMirror> getClassMirrorsByTag(Symbol name) { List<ClassMirror> res = new List<ClassMirror>(); MirrorSystem ms = currentMirrorSystem(); ms

Create an instance of an object from a String in Dart?

和自甴很熟 提交于 2019-11-27 07:04:35
问题 How would I do the Dart equivalent of this Java code? Class<?> c = Class.forName("mypackage.MyClass"); Constructor<?> cons = c.getConstructor(String.class); Object object = cons.newInstance("MyAttributeValue"); (From Jeff Gardner) 回答1: The Dart code: ClassMirror c = reflectClass(MyClass); InstanceMirror im = c.newInstance(const Symbol(''), ['MyAttributeValue']); var o = im.reflectee; Learn more from this doc: http://www.dartlang.org/articles/reflection-with-mirrors/ (From Gilad Bracha) 回答2:

Dynamic class method invocation in Dart

会有一股神秘感。 提交于 2019-11-27 06:48:50
问题 Like the question at Dynamic class method invocation in PHP I want to do this in Dart. var = "name"; page.${var} = value; page.save(); Is that possible? 回答1: There are several things you can achieve with Mirrors. Here's an example how to set values of classes and how to call methods dynamically: import 'dart:mirrors'; class Page { var name; method() { print('called!'); } } void main() { var page = new Page(); var im = reflect(page); // Set values. im.setField("name", "some value").then((temp)