Reputation: 727
Suppose I have a class like this:
abstract class Foo {
String name bar = 'bar';
}
Using mirror, can we get the value of bar
?
Upvotes: 6
Views: 1316
Reputation: 11171
General answers:
super.field
if you're extending a class and want to access its fields/getters.Since you were looking at Dado, let me explain more how it works, even though it's not a general answer to this question.
First, the approach with Dado was and is pretty experimental. I was trying to use the language features to require a declarative definition of modules so that authors couldn't write modules that didn't work with code-generation. There have been some changes in Dart and mirrors that make this approach less workable, or at least makes it require more boilerplate, so I'm rethinking the whole thing. Take any technique you find there with a gain or two of salt :)
For anyone else who hasn't looked at a sample or the code, in Dado you declare an abstract class for a module definition. A field with a value is declaring a fixed singleton value, similar to how a more traditional DI container might have you write bind(Foo).toValue(new Foo())
.
The way I (used to) get the value is by instantiating the abstract class via mirrors, and simply then simply read the field. dart:mirrors changed to disallow instantiating abstract classes, so this no longer works. This is unfortunate, because other types of bindings were declared with abstract methods, and those cause warnings in non-abstract classes.
Upvotes: 1