MEF's Asp.net Hello World Program

Source: Internet
Author: User

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

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.