dart-mirrors

Instantiate a class from a string

与世无争的帅哥 提交于 2019-11-27 05:07:22
In dart is it possible to instantiate a class from a string? For example: vanilla in javascript: var myObject = window[classNameString]; Objective-C: id myclass = [[NSClassFromString(@"MyClass") alloc] init]; You need to know the library name and class name to make things work properly. Assume you know both, the below example will instantiate the TestClass and call doStuff on it. library test; import "dart:mirrors"; class TestClass { doStuff() => print("doStuff was called!"); } main() { MirrorSystem mirrors = currentMirrorSystem(); LibraryMirror lm = mirrors.libraries['test']; ClassMirror cm =

how to invoke class form dart library string or file

浪子不回头ぞ 提交于 2019-11-26 21:52:49
问题 who to invoke class form dart library string or file? for example for-load.dart file class TestLoad { void requestHandler(){ } } then main.dart file main(){ //this get load lib var lib = currentMirrorSystem().libraries[Uri.parse('dart:core')]; //who to invoke class form TestLoad or for-load.dart? //like java Class.forName('TestLoad') , nodejs require('for-load') } thanks! 回答1: These symbols are the names of the Library, the Class and the constructor for the Class that you want to dynamically

Refer classes by their metadata tag

断了今生、忘了曾经 提交于 2019-11-26 18:38:00
问题 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); } 回答1: 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