I. Overview of the Presentation
This example shows how to implement a custom catalog class that can filter the exported parts using the extension mechanism of the MEF-provided catalog (catalog). Mainly by inheriting the Composablepartcatalog base class, and implementing the interface inotifycomposablepartcatalogchanged to complete.
Related Download (screen recording): Http://yunpan.cn/cVkvuUNfuDtTX access password 567d
Warm tip: If the screen recording and code can not be downloaded, you can leave a message in the station, or send an email to [email protected]
Welcome to be interested in research. NET related technology users add QQ Group: 18507443
Second, custom Parts Catalog category Catalog
In MEF, you can define your own catalog classes in addition to the directory classes that you provide to inject aggregatecatalog, Assemblycatalog, Directorycatalog, and so on.
Custom directory classes need to inherit from the Composablepartcatalog class and implement Interface inotifycomposablepartcatalogchanged. The code looks like the following:
[CSharp]View PlainCopy
- Public class Filteredcatalog:composablepartcatalog, inotifycomposablepartcatalogchanged
- {
- #region Private Fields
- private readonly composablepartcatalog M_composablepartcatalog;
- private readonly inotifycomposablepartcatalogchanged m_notifycomposablepartcatalogchanged;
- private readonly iqueryable<composablepartdefinition> m_parts;
- #endregion
- #region Constructors
- // <summary>
- /// default constructor.
- // </summary>
- /// <param name= "composablepartcatalog" > directory catalog containing all exported parts. </param>
- /// <param name= "expression" > Filter criteria expression. </param>
- Public Filteredcatalog (Composablepartcatalog composablepartcatalog, expression<func< Composablepartdefinition, bool>> expression)
- {
- M_composablepartcatalog = Composablepartcatalog;
- m_notifycomposablepartcatalogchanged = Composablepartcatalog as inotifycomposablepartcatalogchanged;
- M_parts = composablePartCatalog.Parts.Where (expression);
- }
- #endregion
- #region inotifycomposablepartcatalogchanged
- // <summary>
- /// component catalog The event that was triggered after the catalogs have changed.
- // </summary>
- public Event eventhandler<composablepartcatalogchangeeventargs> Changed
- {
- Add
- {
- if (m_notifycomposablepartcatalogchanged! = null)
- {
- M_notifycomposablepartcatalogchanged.changed + = value;
- }
- }
- Remove
- {
- if (m_notifycomposablepartcatalogchanged! = null)
- {
- M_notifycomposablepartcatalogchanged.changed-= value;
- }
- }
- }
- // <summary>
- /// The event that is triggered when the catalog catalog is changing.
- // </summary>
- public event eventhandler<composablepartcatalogchangeeventargs> changing
- {
- Add
- {
- if (m_notifycomposablepartcatalogchanged! = null)
- {
- M_notifycomposablepartcatalogchanged.changing + = value;
- }
- }
- Remove
- {
- if (m_notifycomposablepartcatalogchanged! = null)
- {
- M_notifycomposablepartcatalogchanged.changing-= value;
- }
- }
- }
- #endregion
- #region Composablepartcatalog
- // <summary>
- /// Gets the part definition contained in the catalog. After the expression in the constructor has been filtered, it is already part of the exported part in the Directory Catalog object.
- // </summary>
- public override iqueryable<composablepartdefinition> Parts
- {
- get { return m_parts;}
- }
- #endregion
- }
The above code contains the following points, as a general rule:
1, the constructor passed the base directory catalog object, this directory object may contain a lot of export parts, we want to implement the Directory filter class Filteredcatalog is based on this directory to filter, it is a complete, and Filteredcatalog is a subset of it. Another parameter is the filter expression, where the filter is written by the caller, and the internal filtering method is actually the where () approach provided by LINQ.
2, for the implementation of interface inotifycomposablepartcatalogchanged, is actually associated with the event of the base directory catalog object, that is, when the underlying directory object changes, The directory filter class Filteredcatalog will also be notified accordingly.
3. Overrides the Parts collection property of the base class Composablepartcatalog, which returns the part definition that is contained in the directory, and the part definition that needs to be exposed in the catalog is returned through the collection. Therefore, the above code returns the filtered part definition through this property.
The filter class is defined and the next step is how to use it.
Iii. using the Custom catalog class catalog
As shown in the following code:
[CSharp]View PlainCopy
- Get the parts you want.
- Directorycatalog Catalog = new Directorycatalog ("controls");
- Compositioncontainer container = new Compositioncontainer (catalog);
- Filter Catalog to create sub-assembly containers.
- Filteredcatalog Filteredcatalog = new filteredcatalog (catalog,
- O=>o.metadata.containskey ("UC") && o.metadata["UC"]. ToString () = = "BB");
- Compositioncontainer Filteredcontainer = new Compositioncontainer (Filteredcatalog, container);
- UserControl UserControl = filteredcontainer.getexportedvalue<usercontrol> ();
- This. Maincontentcontrol.content = UserControl;
First, through the Directorycatalog class, get all the part definitions in the Controls subfolder in the application root and generate the top-level combo container container (type Compositioncontainer).
Then use the custom Catalog filter class Filteredcatalog to filter the part definitions in the Directorycatalog directory and generate the sub-combo container filteredcontainer (type Compositioncontainer).
Finally, the export part of the specified protocol type is obtained by combining the getexportedvalue<t> () method of the container. It is necessary to note that an exception is thrown if there are no exported parts of the corresponding protocol type in the composition container.
Complete sample Code and screen recording files can be obtained from the following address.
Iv. Related Resources
1. MSDN Official information: http://msdn.microsoft.com/zh-cn/library/dd460648 (v=vs.110). aspx
2, referring to the Microsoft MVP Bēniaǒ article "MEF Programming Guide VII: Use directory (catalog) dynamic loading XAP and directory filtering (Filtered catalog)", Access address: http://www.cnblogs.com/beniao/ Archive/2010/07/26/1782622.html
[MEF] the NO. 05 MEF Catalog (catalog) filter