Skip to content

Instantly share code, notes, and snippets.

@pietergreyling
Forked from anonymous/main.dart
Last active November 15, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pietergreyling/6557c9e09fe93e2ff97726546b9c223a to your computer and use it in GitHub Desktop.
Save pietergreyling/6557c9e09fe93e2ff97726546b9c223a to your computer and use it in GitHub Desktop.
Dart reflection using the 'dart:mirrors' package

Dart Reflection using Dart Mirrors

For background on Reflection with Mirrors in Dart see:

Reflection in Dart with Mirrors: An Introduction - Gilad Bracha (November 2012 (updated November 2013))

Originally created on https://dartpad.dartlang.org as https://dartpad.dartlang.org/53dae7eab07f4fad158e8fed3d62c1d5

Contains no errors and warnings. This code has no associated html or css. It imports the 'dart:mirrors' packages as well.

Find this at dartpad.dartlang.org/?source=c69e51e8-874a-4cda-84c9-a43d0e661cdf.

Created with <3 with dartpad.dartlang.org.

Example output:

HTML OUTPUT CONSOLE

>> [ .SelfPrinting ] - - - - -
>> + Declarations - - - - -
dummyFunc11
dummyFunc12
dummyFunc13
SelfPrinting
>> + Members - - - - -
==
hashCode
toString
noSuchMethod
runtimeType
dummyFunc01
dummyFunc02
dummyFunc11
dummyFunc12
dummyFunc13
>> + Constructor - - - - -
SelfPrinting
>> + Parameters - - - - -
int: arg1
import 'dart:mirrors' show
MirrorSystem, ClassMirror, ParameterMirror, DeclarationMirror, MethodMirror, reflectClass;
class SuperClass {
SuperClass(){ /* pass */ }
dummyFunc01(){ /* pass */ }
dummyFunc02(){ /* pass */ }
}
class SelfPrinting
extends SuperClass {
SelfPrinting(int arg1){ /* pass */ }
dummyFunc11(){ /* pass */ }
dummyFunc12(){ /* pass */ }
dummyFunc13(int i){ /* with arg(s): pass */ }
}
main() {
ClassMirror classMirror = reflectClass(SelfPrinting);
print(">> [ ${MirrorSystem.getName(classMirror.qualifiedName)} ] - - - - -");
print(">> + Declarations - - - - -");
classMirror.declarations.forEach((symbol, declarationMirror) {
print(MirrorSystem.getName(symbol));
});
print(">> + Members - - - - -");
classMirror.instanceMembers.forEach((symbol, methodMirror) {
print(MirrorSystem.getName(symbol));
});
// find contructors in declarations map
List<DeclarationMirror> constructors = new List.from(
classMirror.declarations.values.where((declare) {
return declare is MethodMirror && declare.isConstructor;
})
);
// cast DeclarationMirror into MethodMirror and use getter MethodMirror.parameters
// to get all parameters of the constructor.
constructors.forEach((constructor) {
if (constructor is MethodMirror) {
print(">> + Constructor - - - - -");
String name = MirrorSystem.getName(constructor.simpleName);
print("$name");
List<ParameterMirror> parameters = constructor.parameters;
print(">> + Parameters - - - - -");
parameters.forEach((parameter) {
String type = MirrorSystem.getName(parameter.type.simpleName);
String name = MirrorSystem.getName(parameter.simpleName);
print("$type: $name");
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment