1. Learn about MEF
2. Simple demo
3.CodeDownload
1. Learn about MEF
MEF is an open-source project on codeplex. Address: http://mef.codeplex.com/, project objective: simplifying the design of extensible applications and components.
2. Simple demo
2.1 create an Asp.net web application and add the system. componentmodel. Composition application.
2.2 Add a web form page aspnetmefbasic. aspx. The background code is as follows:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
NamespaceMEF
{
UsingSystem. componentmodel. composition;
UsingSystem. componentmodel. Composition. Hosting;
Public Partial ClassAspnetmefbasic: system. Web. UI. Page
{
[Import]
Class1 C1;
Protected Void Page_load ( Object Sender, eventargs E)
{
// Step 1:
// Find the Assembly (. dll) that has the stuff
// We need (I. e. [Export] Ed stuff) and put it in our catalog
Directorycatalog catalog =
New Directorycatalog (system. Io. Path. Combine (appdomain. currentdomain. basedirectory, " Bin " ));
// Step 2:
// To do anything with the stuff in the catalog, we need
// Put into a container (which has methods to do the magic stuff)
Compositioncontainer container =
New Compositioncontainer (Catalog );
//Step 3:
//Now lets do the magic bit-wiring everything up
Container. composeparts (This);
//Step 4:
//Lets see if it works
Div1.innertext=C1.s1;
}
}
}
2.3 Add a class class1 with the following code:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Namespace MEF
{
Using System. componentmodel. composition;
[Export]
Public Class Class1
{
Public String S1 = " Hello World " ;
}
}
2.4 run the aspnetmefbasic. aspx webpage. The result is as follows:
3. Download Code
/Files/xuqiang/mef.rar