How to Understand Elements
We can understand metadata as additional information exported along with the type.. Sometimes we will consider exporting metadata along with the type and adding some instructions so that we can add some filtering conditions during the import.
Does the default type export metadata?
The above content is concise. Maybe you don't understand it very well. In programming, we can understand many things after writing code. Therefore, my theoretical skills are relatively poor. What I am not good at is a long article. Let's take a look at the code.
First, we need to figure out the problem: I have not manually added the default export type of metadata, and whether it has metadata. To make the code simpler, I will export a class directly without writing public interfaces.
Here we will write a class directly, and then directly import this type:
The sample code is set in the current Assembly and can be found in the AssemblyCatalog range.
The Parts attribute of AssemblyCatalog does not appear in the smart prompt (inherited from the ComposablePartCatalog class, probably because the virtual method is not overwritten, so it is not displayed in the smart prompt ), however, this attribute does exist. by enumerating Parts, you can access each exported component class. In the ExportDefinitions attribute, each ExportDefinition object has a Metadata attribute, which is the Metadata of each export, it is of the dictionary type (IDictionary <string, object>), key is of the string type, and value is any Object ).
After running the application, we will see the following content:
This example shows that, by default, the export is metadata. From the preceding running result, we can guess that the default metadata is used to describe the type of the export component.
How to export metadata
To export metadata, in addition to applying the ExportAttribute feature to the target type, you also need to use the ExportMetadataAttribute feature to define the number of elements. The dictionary structure is followed when defining, that is, the two parameters of the constructor represent the key and value respectively. The following code is used:
The two metadata marks the version of this component as 200, and the author is John. We know that metadata is an IDictionary <string, object> dictionary structure, which is easy to handle. We use Lazy <T, TMetadata> during import. We used Lazy <T> before, now, with metadata, Lazy <T, TMetadata> is used, and the TMetadata type is set to IDictionary <string, object>. The sample code is as follows:
Okay. Run it. We have imported the metadata as well.
Another more complicated method of metadata export and import is self-implemented strong metadata. Let's do it.
Defines a public interface for metadata, named ICustMetadata.
When defining an interface, add the MetadataViewImplementation feature and specify which classes will implement this interface.
2. The class implementing ICustMetadata is MyCustMetaData, so we need to define this class.
4. Modify the called Code as follows:
Finally, the following result is displayed:
The actual type of metadata is exactly the MyCustMetaData class defined above.
So far today, ladies and gentlemen.