Skip to content

Instantly share code, notes, and snippets.

@xiongxin
Last active October 26, 2015 07:58
Show Gist options
  • Save xiongxin/4da113d60472ded7e108 to your computer and use it in GitHub Desktop.
Save xiongxin/4da113d60472ded7e108 to your computer and use it in GitHub Desktop.
Retrieve metadata in Dartlang
// Dart VM version: 1.13.0-dev.7.3 (Fri Oct 23 04:20:51 2015) on "windows_x64"
// https://www.dartlang.org/docs/dart-up-and-running/ch03.html#dartmirrors---reflection
// http://stackoverflow.com/questions/19492607/how-to-retrieve-metadata-in-dartlang
import 'dart:mirrors';
class todo {
final String who;
final String what;
const todo(this.who, this.what);
}
@todo('akira', 'add something')
class Foo {
@todo('naoto', 'do something')
String fooVariable;
@todo('kitano', 'change the name')
void fooMethod() {
}
Foo() {
ClassMirror cm = reflectClass(Foo);
for (VariableMirror vm in cm.declarations.values.where((m) => m is VariableMirror)) {
print(vm.metadata.first.reflectee.what);
}
for (MethodMirror vm in cm.declarations.values.where((m) => m is MethodMirror && !m.isConstructor)) {
print(vm.metadata.first.reflectee.what);
}
}
}
void main() {
Foo foo = new Foo();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment