5

谁来调用类表单 dart 库字符串或文件?

例如

for-load.dart 文件

class TestLoad {
  void requestHandler(){
  }
}

然后是 main.dart 文件

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')
}

谢谢!

4

2 回答 2

4

这些符号是您要动态调用的库、类和类的构造函数的名称

飞镖

library foo_library;

class Foo {
  String bar;
}

调用类.dart

library new_instance_test;

import "dart:mirrors";
import "foo.dart";

int main() {
  // These symbols are the names of the Library, the Class and the constructor for the Class that you want to dynamically load
  final Symbol librarySymbol = const Symbol("foo_library");
  final Symbol classSymbol = const Symbol("Foo");
  final Symbol constructorSymbol = const Symbol("");

  MirrorSystem mirrorSystem = currentMirrorSystem();

  // Get LibraryMirror for Library foo_library.
  // It returns an iterator, get the first LibraryMirror
  LibraryMirror libraryMirror = mirrorSystem.findLibrary(librarySymbol).first;

  // Get ClassMirror for Class Foo
  ClassMirror classMirror = libraryMirror.declarations[classSymbol];

  // Get the InstanceMirror using the default constructor
  InstanceMirror testClassInstanceMirror = classMirror.newInstance(constructorSymbol, []);

  //Get the reflectee object from the InstanceMirror
  Foo foo = testClassInstanceMirror.reflectee;

  //Set bar and print it
  foo.bar = "foobar";
  print(foo.bar);
}
于 2013-10-03T18:49:36.617 回答
0

正如评论中提到的那样,对您可能非常有用的东西是孤立的。如果你想看看它们,你应该看看Seth Ladd的这篇博文。我还将深入研究该主题,看看我是否可以让事情顺利进行。

于 2014-07-02T06:42:13.793 回答