dart-mirrors

Is there a way to dynamically call a method or set an instance variable in a class in Dart?

我只是一个虾纸丫 提交于 2019-12-23 15:42:44
问题 I would want to be able to do something like this with a Dart class constructor: class Model { // ... setting instance variables Model(Map fields) { fields.forEach((k,v) => this[k] = v); } } Obviously, this doesn't work, because this doesn't have a []= method. Is there a way to make it work or is it simply not "the dart way" of doing things? If it's not, could you show me what would be the right way to tackle this? 回答1: Currently no. You will have to wait for reflection to arrive in Dart

What is the format of the “arguments” parameter to ClassMirror.newInstance()?

帅比萌擦擦* 提交于 2019-12-23 04:48:10
问题 I'm perfectly willing to play with this until I get it right, but was hoping someone might give me a hint. The parameter is declared in the docs (gen-dartdocs/dart-mirrors/ClassMirror/newInstance.html) as InstanceMirror newInstance(Symbol constructorName, List positionalArguments, [Map<Symbol,dynamic> namedArguments]); There is a nice writeup on the format of positionalArguments and namedArguments in the docs. However, it is just a little on the abstract side of my current tolerance level. A

Compiled AngularDart fails with error in dynamic_injector

无人久伴 提交于 2019-12-23 02:18:30
问题 I have been trying to make AngularDart work but I always get exceptions for undefined objects. @MirrorsUsed( targets: const [ 'angular.core', 'angular.core.dom', 'angular.core.parser', 'angular.routing', 'angular.core.zone', 'di.di', 'di.dynamic_injector', NodeTreeSanitizer, DynamicParser, DynamicParserBackend, Injector ], metaTargets: const [ NgInjectableService, NgComponent, NgDirective, NgController, NgFilter, NgAttr, NgOneWay, NgOneWayOneTime, NgTwoWay, NgCallback, NgZone ], override: '*'

How to use dart to implement a delegate/proxy?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 07:47:50
问题 I have two classes Parser and Proxy , and when I invoke a method from Parser , which does not exist, it will delegate it to Proxy class. My code: class Parser { noSuchMethod(Invocation invocation) { // how to pass the `invocation` to `Proxy`??? } } class Proxy { static String hello() { return "hello"; } static String world() { return "world"; } } That when I write: var parser = new Parser(); print(parser.hello()); It will print: hello 回答1: You have to use dart:mirrors . Here's how to do :

How to use dart to implement a delegate/proxy?

删除回忆录丶 提交于 2019-12-22 07:45:10
问题 I have two classes Parser and Proxy , and when I invoke a method from Parser , which does not exist, it will delegate it to Proxy class. My code: class Parser { noSuchMethod(Invocation invocation) { // how to pass the `invocation` to `Proxy`??? } } class Proxy { static String hello() { return "hello"; } static String world() { return "world"; } } That when I write: var parser = new Parser(); print(parser.hello()); It will print: hello 回答1: You have to use dart:mirrors . Here's how to do :

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

橙三吉。 提交于 2019-12-22 04:53:15
问题 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! 回答1: 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(),

how to enable --enable-experimental-mirrors in dart build?

瘦欲@ 提交于 2019-12-19 08:08:17
问题 My build of my projects are failing because they rely on mirrors and dart build out put tells me to use --enable-experimental-mirrors to try to use mirrors in dart2js code as it is. so if I run pub build --enable-experimental-mirrors all I get is Could not find an option named "enable-experimental-mirrors" . Any hints much appreciated. 回答1: I haven't tried this myself yet but maybe you can pass it as a command line option in the transformer config transformers: - $dart2js: commandLineOptions:

how to enable --enable-experimental-mirrors in dart build?

拥有回忆 提交于 2019-12-19 08:08:09
问题 My build of my projects are failing because they rely on mirrors and dart build out put tells me to use --enable-experimental-mirrors to try to use mirrors in dart2js code as it is. so if I run pub build --enable-experimental-mirrors all I get is Could not find an option named "enable-experimental-mirrors" . Any hints much appreciated. 回答1: I haven't tried this myself yet but maybe you can pass it as a command line option in the transformer config transformers: - $dart2js: commandLineOptions:

in Dart, using Mirrors, how would you call a class's static method from an instance of the class?

情到浓时终转凉″ 提交于 2019-12-18 09:07:05
问题 if I have an instance, and I know the instance's class contains a static method named statFn() , how do I call statFn() from the instance? for example, abstract class Junk { ... } class Hamburger extends Junk { static bool get lettuce => true; ... } class HotDog extends Junk { static bool get lettuce => false; ... } Junk food; // either Hamburger or Hotdog // how do you get lettuce of food's type? 回答1: This should get you started. findStaticAndInvoke() looks at the class of the provided

Instantiate a class from a string

微笑、不失礼 提交于 2019-12-17 07:38:04
问题 In dart is it possible to instantiate a class from a string? For example: vanilla in javascript: var myObject = window[classNameString]; Objective-C: id myclass = [[NSClassFromString(@"MyClass") alloc] init]; 回答1: You need to know the library name and class name to make things work properly. Assume you know both, the below example will instantiate the TestClass and call doStuff on it. library test; import "dart:mirrors"; class TestClass { doStuff() => print("doStuff was called!"); } main() {