MEF的asp.net Hello World程式

來源:互聯網
上載者:User

1.瞭解MEF
2. 簡單的demo
3. 代碼下載

1. 瞭解MEF
 
mef是codeplex上的開源項目,地址:http://mef.codeplex.com/,工程目的: simplifying the design of extensible applications and components. 

2. 簡單demo

2.1 建立asp.net web application,添加 System.ComponentModel.Composition的應用。

2.2 添加一個web form頁面aspnetMEFBasic.aspx,後台代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MEF
{
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;

    public partial class aspnetMEFBasic : 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 to 
            // 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);

            //Step4:
            //Lets see if it works
            div1.InnerText = c1.s1;
        }
    }

2.3 添加類Class1,代碼如下:

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 運行aspnetMEFBasic.aspx網頁,結果如下:

3. 代碼下載

/Files/xuqiang/MEF.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.