Вам нужно знать имя библиотеки и имя класса, чтобы все работало правильно. Предположим, вы знаете оба, приведенный ниже пример будет создавать экземпляр TestClass
и вызывать doStuff
на нем.
library test;
import "dart:mirrors";
class TestClass {
doStuff() => print("doStuff was called!");
}
main() {
MirrorSystem mirrors = currentMirrorSystem();
LibraryMirror lm = mirrors.libraries['test'];
ClassMirror cm = lm.classes['TestClass'];
Future tcFuture = cm.newInstance('', []);
tcFuture.then((InstanceMirror im) {
var tc = im.reflectee;
tc.doStuff();
});
}
Несколько замечаний об этом решении:
- The library
test
we are trying to load the class from is already imported in the VM, which makes this case a bit easier.
- the call the
newInstance
allows for passing parameters to the constructor. Positional arguments are implemented, but named parameters are not yet implemented (as of the M2 release).
newInstance
returns a Future to allow it to work across isolates.