dart-mirrors

How to get concrete object of a static method via mirror API?

旧巷老猫 提交于 2019-12-10 03:42:07
问题 I have something like this: class MyClass { static void DoSomething(arg1, arg2){...} } Via reflection, I am able to get the ClassMirror of this class. From this point, how would I get to the concrete static function so I can call it. Note that I tried to use: ObjectMirror.invoke('DoSomething', [arg1, arg2]); which would initially appear to work, but it doesn't support passing of complex types as arguments, This static function requires a complex type as one of it's arguments. Ideally, I'd

In Dart, can you retrieve metadata (e.g., annotations) at runtime using reflection?

落爺英雄遲暮 提交于 2019-12-08 07:42:28
问题 If so, how is this accomplished? If not, are there any plans to support this in future Dart releases? I'm mostly referring to your own created custom annotations. In this documentation link, https://www.dartlang.org/docs/spec/latest/dart-language-specification.html#h.d0rowtffuudf, it says: "Metadata is associated with the abstract syntax tree of the program construct p that immediately follows the metadata, assuming p is not itself metadata or a comment . Metadata can be retrieved at runtime

Getting ClassMirror instances for all classes that have an annotation

两盒软妹~` 提交于 2019-12-08 04:32:29
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 libraryMirror.declarations.values){ if(classMirror.metadata!=null){ for(var meta in classMirror.metadata){ if(meta

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

只谈情不闲聊 提交于 2019-12-06 11:27:37
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? 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 ClassMirror); var cm = myClasses.firstWhere((cm) => cm.simpleName == symbol); var instance = cm.newInstance(const

custom annotation / Metadata in dart lang

爱⌒轻易说出口 提交于 2019-12-06 05:01:06
问题 Can any one explain me the use of annotations in Dart? In the documentations, I found this example: library todo; class todo { final String who; final String what; const todo(this.who, this.what); } followed by import 'todo.dart'; @todo('seth', 'make this do something') void doSomething() { print('do something'); } so, what shall I write in the main() to get the doSomething() function executed? thanks 回答1: Something like import 'dart:mirrors'; import 'do_something.dart'; import 'todo.dart';

How can i test the existence of a function in Dart?

懵懂的女人 提交于 2019-12-05 07:05:32
Is there a way to test the existence of a function or method in Dart without trying to call it and catch a NoSuchMethodError error? I am looking for something like if (exists("func_name")){...} to test whether a function named func_name exists. Thanks in advance! You can do that with mirrors API : import 'dart:mirrors'; class Test { method1() => "hello"; } main() { print(existsFunction("main")); // true print(existsFunction("main1")); // false print(existsMethodOnObject(new Test(), "method1")); // true print(existsMethodOnObject(new Test(), "method2")); // false } bool existsFunction(String

How to get concrete object of a static method via mirror API?

自作多情 提交于 2019-12-05 05:35:51
I have something like this: class MyClass { static void DoSomething(arg1, arg2){...} } Via reflection, I am able to get the ClassMirror of this class. From this point, how would I get to the concrete static function so I can call it. Note that I tried to use: ObjectMirror.invoke('DoSomething', [arg1, arg2]); which would initially appear to work, but it doesn't support passing of complex types as arguments, This static function requires a complex type as one of it's arguments. Ideally, I'd like to get the 'Function' object that represents the static method so I can invoke it directly. Gilad

Reading static files under a library in Dart?

可紊 提交于 2019-12-04 16:57:07
I am writing a library in Dart and I have static files under the library folder. I want to be able to read those files, but I'm not sure how to retrieve the path to it... there is not __FILE__ or $0 like in some other languages. Update: It seems that I was not clear enough. Let this help you understand me: test.dart import 'foo.dart'; void main() { print(Foo.getMyPath()); } foo.dart library asd; class Foo { static Path getMyPath() => new Path('resources/'); } It gives me the wrong folder location. It gives me the path to test.dart + resources/ , but I want the path to foo.dart + resources/ .

How to tell if an object is an instance of a class

大憨熊 提交于 2019-12-03 10:52:24
问题 How can I determine whether an object is of a class or not in the Dart language? I'm looking to do something like the following: if (someObject.class.toString() == "Num") { ... } And what is the returned value type? Will it have to be a String? The mirror library has been up and down and seems to be subject to rapid change right now, as the one thing I did find simply did not work as shown. 回答1: By using the is and is! operators, like this: if (someObject is T) From the documentation: The is

How to tell if an object is an instance of a class

筅森魡賤 提交于 2019-12-03 01:24:20
How can I determine whether an object is of a class or not in the Dart language? I'm looking to do something like the following: if (someObject.class.toString() == "Num") { ... } And what is the returned value type? Will it have to be a String? The mirror library has been up and down and seems to be subject to rapid change right now, as the one thing I did find simply did not work as shown. Eliran Malka By using the is and is! operators, like this: if (someObject is T) From the documentation : The is and is! operators are handy for checking types. The result of obj is T is true if obj