Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate a class from a string

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];
like image 749
1dayitwillmake Avatar asked Jan 09 '13 17:01

1dayitwillmake


2 Answers

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 = lm.classes['TestClass'];
  Future tcFuture = cm.newInstance('', []);
  tcFuture.then((InstanceMirror im) {
    var tc = im.reflectee;
    tc.doStuff();
  });
}

A few notes about this solution:

  1. The library test we are trying to load the class from is already imported in the VM, which makes this case a bit easier.
  2. 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).
  3. newInstance returns a Future to allow it to work across isolates.
like image 89
Kyrra Avatar answered Sep 29 '22 06:09

Kyrra


The syntax has changed. I got it working this way

library test;

import "dart:mirrors";

class TestClass {
  doStuff() => print("doStuff was called!");
}

main() {
  MirrorSystem mirrors = currentMirrorSystem();
  LibraryMirror lm = mirrors.libraries.values.firstWhere(
      (LibraryMirror lm) => lm.qualifiedName == new Symbol('test'));

  ClassMirror cm = lm.declarations[new Symbol('TestClass')];

  InstanceMirror im = cm.newInstance(new Symbol(''), []);
  var tc = im.reflectee;
  tc.doStuff();
}

If there are more libraries named 'test' this will fail though.

like image 37
Günter Zöchbauer Avatar answered Sep 29 '22 05:09

Günter Zöchbauer