dart-mirrors

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

我怕爱的太早我们不能终老 提交于 2020-01-11 13:39:43
问题 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

Obtain getters and/or attribuites from ClassMirror using reflection in Dart?

怎甘沉沦 提交于 2020-01-06 01:58:26
问题 Previous version of dart were able to get getters using cm.getters.values As is posted in this answer: https://stackoverflow.com/a/14505025/2117440 However actual version was removed that featuread and replaced by cm.declarations.values last code gets all attributes, getters, setters, methods and constructor. I would like to know if there is a way to get only "getters and attributes" without others method. The code that I'm using right now is that one: import "dart:mirrors"; class

How to retrieve annotations on declarations

梦想与她 提交于 2020-01-05 09:22:47
问题 According to the spec, metadata can appear before a variable declaration. However, it does not say anything about if this is possible to retrieve. const annotation = null; main() { @annotation var a = ""; print(reflect(a) is DeclarationMirror); } Outputs false ; Is the retrieval of such usage of annotations possible? 回答1: Sorry for the anwser, but actually, it's not possible to do what you want. For a simple reason : Your code is syntaxicly correct, but no instance of the annotation is

How to retrieve annotations on declarations

笑着哭i 提交于 2020-01-05 09:22:38
问题 According to the spec, metadata can appear before a variable declaration. However, it does not say anything about if this is possible to retrieve. const annotation = null; main() { @annotation var a = ""; print(reflect(a) is DeclarationMirror); } Outputs false ; Is the retrieval of such usage of annotations possible? 回答1: Sorry for the anwser, but actually, it's not possible to do what you want. For a simple reason : Your code is syntaxicly correct, but no instance of the annotation is

Using mirrors, how can I get a reference to a class's method?

独自空忆成欢 提交于 2020-01-04 04:56:02
问题 Say I have an instance of a class Foo , and I want to grab a list of all of its methods that are annotated a certain way. I want to have a reference to the method itself, so I'm not looking to use reflection to invoke the method each time, just to grab a reference to it the first time. In other words, I want to do the reflection equivalent of this: class Foo { a() {print("a");} } void main() { var f = new Foo(); var x = f.a; // Need reflective way of doing this x(); // prints "a" } I have

How can I get the concrete type of a generic type variable using mirrors in Dart?

自闭症网瘾萝莉.ら 提交于 2020-01-04 02:51:09
问题 Assuming I have a List of String s like this. var myList = new List<String>(); How can I figure out that myList is a List of String s using mirrors? I tried it using the typeVariables of ClassMirror but the mirror seems to just describe the gerneric List class. InstanceMirror im = reflect(myList); // InstanceMirror on instance of 'List' ClassMirror cm = im.type; // ClassMirror on 'List' print(cm.typeVariables['E']) // TypeVariableMirror on 'E' I also found this in the documentation but I have

dart, how to define a class so it can be used as a class attribute?

為{幸葍}努か 提交于 2020-01-02 12:17:42
问题 I've seen in polymer.dart they have: class CustomTag { final String tagName; const CustomTag(this.tagName); } but how does that interact with the rest of the code? from just the code above I can't see how using @CustomTag('my-tag') actually does anything but creates a CustomTag which is then garbage collected since nothing is referencing it. 回答1: To answer the question in the title; these are called Annotations; they are simply const constructors. To answer the second question; these are

How to retrieve metadata in Dartlang?

99封情书 提交于 2019-12-30 03:33:09
问题 Dartlang tutorial introduces package:meta https://www.dartlang.org/docs/dart-up-and-running/contents/ch02.html#ch02-metadata DartEditor recognise the metadata as shown at above tutorial. The tutorial also explains how to create custom metadata and retrieve it. But there is no code sample on how to retrieve it. 回答1: Through reading some implementations such as @published metadata in Polymer.dart, I find the way. https://www.dartlang.org/articles/reflection-with-mirrors/ https://api.dartlang

Create an instance of an Generic Class from a String in Dart? [duplicate]

戏子无情 提交于 2019-12-25 05:18:51
问题 This question already has answers here : how to invoke class form dart library string or file (2 answers) Closed 4 years ago . Hello from here I know that I can create an instance from a String: How to get a Class name dynamically (from a string) in Dart, then create an instance? but How do I create an instance of a Generic class from a String, ie: var class_name = "GenericController<Book>"; // user input here new class_name(); 回答1: This is not currently possible (as you probably noticed).

Getting value of a class variable through mirroring via getField

假装没事ソ 提交于 2019-12-24 17:27:16
问题 I am trying to understand how the Mirrors Api works. Specifically, how to obtain the value of a field from its Symbol , using getField . For the getField method, it should work for any Symbol which is a getter, and it might be implicit. I therefore understood this that getField could be called directly on fields. In the following code sample, the getters for a and b should be implictly defined. But the code throws, complainining that it cannot find any getter. Breaking on exception: object of