如何调用类表单dart库字符串或文件
发布时间:2020-12-14 23:22:51 所属栏目:资源 来源:网络整理
导读:谁调用类表单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 for
谁调用类表单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') } 谢谢! 解决方法
这些符号是要动态调用的类的库,类和构造函数的名称
foo.dart library foo_library; class Foo { String bar; } invoke_class.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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |