MEF (4): search range

Source: Internet
Author: User

In the previous article, almost every example involves the search location of the extension class. We may also consider that since it is auto scaling, it will certainly have one or more people to search for. Otherwise, how does the MEF framework know where there are extension components?

Just like we use a navigation system to find all the hotels in a certain place, it is because the hotels in this area have registered relevant information in the database that our navigation system can find it, if a hotel does not provide any data to the navigation database, it is clear that the navigation system cannot identify its detailed address.

MEF usually has three search scopes for extended components:

If you are not enough, you can use AggregateCatalog to set multiple search ranges.

Next we will try it one by one.

Based on the Range Query of an assembly, the most common task is to search in the current Assembly. Of course, you can also search in other programming sets. As long as you can get an Assembly object, you can find it in it. Everyone is so smart. I believe everyone can understand it.

The following is a typical practice. The example is to find extended parts in the current Assembly.

First, we define an interface for standardizing the behavior of Extended parts.

Then, write two classes to implement the above interfaces and export them.

In the Application class, use a public field to import all of them.

Finally, assemble and test the call in Main.

Finally, the program running result is shown in.

 

Ii. DirectoryCatalog

We may be a bit unfamiliar with this one, because we have never used this class in the examples in the previous article, but do not be afraid of it because it is a stranger. How high is the sky, your courage should be too big. unfamiliar things are not complicated in many cases. It is like talking to strangers sometimes is more comfortable than talking to acquaintances.

The DirectoryCatalog class can specify a directory for MEF to search for extension components under this directory. If you need more detailed searches, you can specify a search string in the DirectoryCatalog class constructor, this string is the same as what we usually search for files, for example

This time, we use a WinForm application as an example. In the form, we drag and drop several controls. For details, see.

The control layout is random. I have always advocated personalized layout. The following are some important controls:

The TextBox named txtDirec is used to display the selected directory. The ComboBox control named cmbClass is used to display the list of extended classes found by MEF. the TextBox named txtResult displays the call results of the extended class.

The following describes the general idea of this example. First, we put all the extended class libraries in an arbitrary folder, and then select this directory in the application window, let the MEF framework discover all extension types in this directory and display them in ComboBox. Then we select a class from ComboBox to call and view the call result.

This interface has only one method, SaySomething. We will use several classes to implement this interface. Each class is written to a class library independently.

In the same way, create another two class library projects: ExtLibB and ExtLibC.

Then, we can look at the following execution results.

 

3. AggregateCatalog

One day, I accidentally wrote the new extension class ExtLibD TO THE WinForm project, like this:

Then I thought, how nice it would be if I could retain the first three extension classes and automatically recognize the extension classes I wrote to the WinForm project. Not afraid. Only you can think of the power of. NET. At this time, I will focus on the AggregateCatalog class, which can be used to merge multiple search ranges. The three extensions we just used are searched in a folder, the fourth extension is written to the WinForm project. Remember that we mentioned the AssemblyCatalog class in the previous article. It can search for extension classes in a program set, therefore, we can find the fourth extension class in the program where the WinForm project is located.

That is to say, we combine the AssemblyCatalog range and the DirectoryCatalog range. Therefore, change the code of the previous button#click to the following:

Use AggregateCatalog to combine the directorycatiner and assemblycatiner, and then pass it to the CompositionContainer constructor to combine the search range.

Okay. Run it now to see if the fourth extension is automatically discovered?

Good, the expected results have been achieved. Are you very happy? just have a cup of coffee.

 

I don't know if you will lament the magic of MEF after reading these great prose about MEF. Where can MEF be used? Just remember the two principles:

Well, I'm also thirsty after talking so much. First, have a cup of orange sweat to quench your thirst. Goodbye.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.