I am back with more MEF! This time I am discussing about metadata that could provide additional information about components the implement contracts. These entries about MEF are more of the “journal” kind rather than a “tutorial”. To provide metadata you just need to use the
ExportMetadata attribute. There are two possible ways by which you could access the provided meta data:
(1) Weakly typed – with the help of
(2) Strongly typed – with the help of interfaces.
The following gist demonstrates both these approaches:
In the above gist,
NamedGreeting2 is a variable that defines metadata for use based on
IDictionary<string, object> approach. Since you need both the
INamedGreeting instance along with the meta data,
Lazy initialization is used with the the
Metadata property providing the metadata and the
Value property providing the instance that implements the required contract. Finally, to access the metadata, you need to know the keys and use indexers with the keys to access the values.
The last line of the previous paragraph already indicated the reason for a strongly typed meta data alternative, so as to not remember/type the key names without intellisense! With that said, I am sure you could immediately look at how the arguments (1st argument especially) to the constructor matches to the properties in the interface
That’s it for this post or a journal entry! Let me finish by saying that I “may” choose to expand on this post some time later though!