Mirror API allows you to retrieve metadata about objects and types programmatically (during execution) and to execute methods on objects . It is similar to reflection in .NET or Java.
A typical example is implementing plugin support:
Let's say that you define an IPlugin
interface and want to automatically register with your PluginManager
an instance of each type that implements it, without need to explicitly register each new implementation. Sometimes you even don't know all the plugins in advance, e.g. if users can deploy their own plugins.
You could do it like this (WARNING: I have never used Mirror API, so this is a high level description based on API documentation, not a proper implementation):
- You first use
MirrorSystem.libraries
to getLibraryMirror
instance for each library in thatMirrorSystem
- Then for each
LibraryMirror
you useclasses
property to getClassMirror
for each class in the library - For each
ClassMirror
usesuperinterfaces
to get all implemented interfaces - If any of the implemented interfaces is
IPlugin
you can create an instance of that class (ClassMirror.newInstance
) and register it with plugin manager.
Without mirror API, you would not be able to enumerate all the types, find what interfaces they implement (without creating an object) or create an instance of a type that you don't know about in advance.
To be honest I am not sure what is the current state of mirror API in Dart, I believe that it is still not finished, but I might be wrong.