Create an instance of a class from a string

In dart, can I instantiate a class from a string?

For example:

  • vanilla in javascript:
var myObject = window[classNameString]; 
  • Objective-C:
 id myclass = [[NSClassFromString(@"MyClass") alloc] init]; 
+8
dart dart-mirrors
source share
3 answers

You need to know the library name and class name for everything to work correctly. Suppose you know both, the example below will create an instance of 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 comments about this solution:

  • The test library that we are trying to load from the class has already been imported into the VM, which makes this case a little easier.
  • calling newInstance allows passing parameters to the constructor. Positional arguments are implemented, but named parameters have not yet been implemented (since the release of M2).
  • newInstance returns Future to allow it to work through isolates .
+3
source share

The syntax has changed. I did it like that.

 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.

+4
source share

This problem bothered me until I thought that I could implement a crude from method to handle the conversion of encoded Json or Dart Maps objects / strings to the desired class.

Below is a simple example that also processes null values ​​and accepts JSON (as a string parameter).

 import 'dart:convert'; class PaymentDetail { String AccountNumber; double Amount; int ChargeTypeID; String CustomerNames; PaymentDetail({ this.AccountNumber, this.Amount, this.ChargeTypeID, this.CustomerNames }); PaymentDetail from ({ string : String, object : Map }) { var map = (object==null) ? (string==null) ? Map() : json.decode(string) : (object==null) ? Map() : object; return new PaymentDetail( AccountNumber : map["AccountNumber"] as String, Amount : map["Amount"] as double, ChargeTypeID : map["ChargeTypeID"] as int, CustomerNames : map["CustomerNames"] as String ); } } 

Below is the implementation

  PaymentDetail payDetail = new PaymentDetail().from(object: new Map()); PaymentDetail otherPayDetail = new PaymentDetail().from(object: {"AccountNumber": "1234", "Amount": 567.2980908}); 

Once again, it’s easy and tedious to clone an entire project, but it works for simple cases.

0
source share

All Articles