dart-mirrors

Conditional imports / code for Dart packages

霸气de小男生 提交于 2019-12-12 10:56:33
问题 Is there any way to conditionally import libraries / code based on environment flags or target platforms in Dart? I'm trying to switch out between dart:io 's ZLibDecoder / ZLibEncoder classes and zlib.js based on the target platform. There is an article that describes how to create a unified interface, but I'm unable to visualize that technique not creating duplicate code and redundant tests to test that duplicate code. game_loop employs this technique, but uses separate classes (GameLoopHtml

How to get a Class name dynamically (from a string) in Dart, then create an instance?

时光毁灭记忆、已成空白 提交于 2019-12-12 10:10:15
问题 I would like to be able to do something like this: class MyClass() {...} var class_name = "MyClass"; // user input here new class_name(); // so here, class_name is supposed to be a class constant Can anybody suggest a simple way to do it? 回答1: One way to do it, is: library my_library; import 'dart:mirrors'; void main() { var userInput = 'MyClass'; var symbol = new Symbol(userInput); var myClasses = currentMirrorSystem().findLibrary(#my_library).declarations.values.where((dm) => dm is

How to get the value from metadata, in Dart?

女生的网名这么多〃 提交于 2019-12-11 12:11:58
问题 Dart code: @Table("users") class User {} And the metadata declaration: class Table { final String name; const Table(this.name); } I can get the metadata @Table by following code: var classMirror = reflectClass(User); var metadata = classMirror.metadata; print(metadata); Which prints: [InstanceMirror on Instance of 'Table'] But I don't know how to get the users from it :( 回答1: You can use InstanceMirror.getField : metadata.getField(#name); 来源: https://stackoverflow.com/questions/21555598/how

Retrieving getter values with dart:mirrors reflection

会有一股神秘感。 提交于 2019-12-11 11:26:50
问题 I have the following code (simplified), that uses reflection to iterate a class's fields and getters and output the values. The ContainsGetter class contains a getter, and the ContainsField class contains a simple field. Using dart:mirrors library, I can get the value of the field by using instanceMirror.getField(fieldName) ), but not the getter by using instanceMirror.invoke(fieldName,[]) . The following Dart script (using the build 17463) gives the output below: app script import 'dart

Dart calling a member function by function name [duplicate]

▼魔方 西西 提交于 2019-12-11 11:22:26
问题 This question already has answers here : Dynamic class method invocation in Dart (3 answers) Closed 5 years ago . I am wondering if there is anyway to call a function by its name in dart as in javascript. I would like to do something as such: foo["bar"](); 回答1: I don't want readers to think what the questioner wants isn't possible in Dart, so I'm adding an answer. You need to use Mirrors to call a method if you have its name available as a string. Here is an example: import 'dart:mirrors';

Get private variable by reflection in dart

廉价感情. 提交于 2019-12-10 23:31:52
问题 I would like to get private variable in an object in dart. This variable has no getter so I want to do this with reflection. I try many way but nothing works to me. For exemple, when I do this: var reflection = reflect(this); InstanceMirror field = reflection.getField(new Symbol(fieldName)); I get an error: No getter for fieldName. It's normal because the variable hasn't getter. How can I get this variable ? EDIT with a test code: Here is my reflect test (test variable is a reflectClass

Dart meta programming features

房东的猫 提交于 2019-12-10 21:08:32
问题 Will there be an equivelent of the c# Reflection.Emit namespace in dart? Reflection.Emit has a number of classes that are used to build types at run time and adding properties, configering their getters and setter and building methods and event handlers all at run time, which is really powerfull when it comes to metaprogramming. my idea is about generating my data models at run time and caching them in a map so i can create instances at run time and add new methods and properties to them when

How retrieve function that marks with metadata

好久不见. 提交于 2019-12-10 18:17:54
问题 Look at the following code snippet. import "dart:mirrors"; class meta { final String data; const meta(this.data); } @meta("Tag") doSomething() => print("You have to do something"); void main() { doSomething(); } How can I retrieve functions, that is market with metadata tags? In my example, I want to find out, which method is marked with meta tags. 回答1: you could do something like this: void main() { doSomething(); getMetaData(); } void getMetaData() { LibraryMirror currentLib =

How can I use Reflection (Mirrors) to access the method names in a Dart Class?

孤街浪徒 提交于 2019-12-10 15:12:17
问题 I need to "fetch" the methods in a Dart Class. How can I do this? And I want to be able to call the methods. May I see an example? 回答1: Here's an easy copy-pasteable code sample: import 'dart:mirrors'; import 'dart:io'; main() { var im = reflect(new File('test')); // Retrieve the InstanceMirror of some class instance. im.type.methods.values.forEach((MethodMirror method) => print(method.simpleName)); } Output is: existsSync _delete exists directory _getDecodedLines readAsTextSync

Getting ClassMirror instances for all classes that have an annotation

守給你的承諾、 提交于 2019-12-10 11:34:54
问题 I have this annotation class Target{ final String value; const Target(this.value); } and 2 classes that are annotated with it @Target("/313") class c1{ } @Target("/314") class c2{ } how can i get a List of ClassMirror instances for the classes that have the Target annotation? based on the selected answer that is if i knew what library my calsses exist in var mirrorSystem = currentMirrorSystem(); var libraryMirror = mirrorSystem.findLibrary(#testlib); for(ClassMirror classMirror in