reflect
Returns an [InstanceMirror] reflecting reflectee. If
reflecteeis a function or an instance of a class that has a
callmethod, the returned instance mirror will be a
ClosureMirror`.Note that since one cannot obtain an object from another isolate, this function can only be used to obtain mirrors on objects of the current isolate.
A MirrorSystem
is the main interface used to reflect on a set of associated libraries. At runtime each running isolate has a distinct MirrorSystem
. It is also possible to have a MirrorSystem
which represents a set of libraries which are not running -- perhaps at compile-time. In this case, all available reflective functionality would be supported, but runtime functionality (such as invoking a function or inspecting the contents of a variable) would fail dynamically.
simpleName
getter is for Dart language entity. The simple name is in most cases the identifier name of the entity, such as 'myMethod' for a method, void myMethod() {...}
or 'mylibrary' for a library 'mylibrary';
declaration.
import 'dart:mirrors';
void someFunctionToBeUsedSomeWhere(Function() cb) {
final cm = reflect(cb) as ClosureMirror;
final name = MirrorSystem.getName(cm.function.simpleName);
print('--- Hey I am {$name}! ---');
}
Example:
class HeyFriend{
void static myMethod() {}
}
someFunctionToBeUsedSomeWhere(MyFriend myMethod);
// --- Hey I am {MyMethod}! ---