Reputation: 3620
This may be a silly question, so sorry for asking this, and fyi I'm new to this kind of stuff.
After reading the documentation about mirrors, I can only grasp that the mirrors API is just like copying some instance plus access to some method that I don't know when/why to use.
Anybody care to give me some pointers, some example would be nice.
Upvotes: 0
Views: 266
Reputation: 14591
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):
MirrorSystem.libraries
to get LibraryMirror
instance for each library in that MirrorSystem
LibraryMirror
you use classes
property to get ClassMirror
for each class in the libraryClassMirror
use superinterfaces
to get all implemented interfacesIPlugin
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.
Upvotes: 4