C#可擴充編程之MEF學習筆記(三):匯出類的方法和屬性

來源:互聯網
上載者:User

標籤:service   類的方法   his   catalog   沒有   action   可擴充   foreach   new   

前面說完了匯入和匯出的幾種方法,如果大家細心的話會注意到前面我們匯出的都是類,那麼方法和屬效能不能匯出呢???答案是肯定的,下面就來說下MEF是如何匯出方法和屬性的。

  還是前面的代碼,第二篇中已經提供了下載連結,大家可以下載學習。

  首先來說匯出屬性,因為這個比較簡單,和匯出類差不多,先來看看代碼,主要看我加註釋的地方,MusicBook.cs中的代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel.Composition;namespace MEFDemo{   [Export("MusicBook")]   public class MusicBook : IBookService   {      //匯出私人屬性      [Export(typeof(string))]      private string _privateBookName = "Private Music BookName";      //匯出公有屬性      [Export(typeof(string))]      public string _publicBookName = "Public Music BookName";      public string BookName { get; set; }   }   [Export("MathBook", typeof(IBookService))]   public class MathBook : IBookService   {      public string BookName { get; set; }      public string GetBookName()      {         return "MathBook";      }   }   [Export("HistoryBook", typeof(IBookService))]   public class HistoryBook : IBookService   {      public string BookName { get; set; }      public string GetBookName()      {         return "HistoryBook";      }   }}

program.cs中的代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.ComponentModel.Composition;using System.ComponentModel.Composition.Hosting;namespace MEFDemo{   class Program   {      [ImportMany("MathBook")]      public IEnumerable<object> Services { get; set; }      //匯入屬性,這裡不區分public還是private      [ImportMany]      public List<string> InputString { get; set; }      static void Main(string[] args)      {         Program pro = new Program();         pro.Compose();         if (pro.Services != null)         {            foreach (var s in pro.Services)            {               var ss = (IBookService)s;               Console.WriteLine(ss.GetBookName());            }         }         foreach (var str in pro.InputString)         {            Console.WriteLine(str);         }         Console.Read();      }            private void Compose()      {         var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());         CompositionContainer container = new CompositionContainer(catalog);         container.ComposeParts(this);      }   }}

下面還用foreach遍曆輸出屬性的值,運行即可查看到結果。最後我會附上源碼供大家下載,這裡就不再了。

下面說匯出方法吧,同理無論是公有方法還是私人方法都是可以匯出的,MusicBook代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel.Composition;namespace MEFDemo{   [Export("MusicBook")]   public class MusicBook : IBookService   {      //匯出私人屬性      [Export(typeof(string))]      private string _privateBookName = "Private Music BookName";      //匯出公有屬性      [Export(typeof(string))]      public string _publicBookName = "Public Music BookName";      public string BookName { get; set; }      //匯出公有方法      [Export(typeof(Func<string>))]      public string GetBookName()      {         return "MusicBook";      }      //匯出私人方法      [Export(typeof(Func<int, string>))]      private string GetBookPrice(int price)      {         return "$" + price;      }   }   [Export("MathBook", typeof(IBookService))]   public class MathBook : IBookService   {      public string BookName { get; set; }      public string GetBookName()      {         return "MathBook";      }   }   [Export("HistoryBook", typeof(IBookService))]   public class HistoryBook : IBookService   {      public string BookName { get; set; }      public string GetBookName()      {         return "HistoryBook";      }   }}

program中的代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.ComponentModel.Composition;using System.ComponentModel.Composition.Hosting;namespace MEFDemo{   class Program   {      [ImportMany("MathBook")]      public IEnumerable<object> Services { get; set; }      //匯入屬性,這裡不區分public還是private      [ImportMany]      public List<string> InputString { get; set; }      //匯入無參數方法      [Import]      public Func<string> methodWithoutPara { get; set; }      //匯入有參數方法      [Import]      public Func<int,string> methodWithPara { get; set; }      static void Main(string[] args)      {         Program pro = new Program();         pro.Compose();         if (pro.Services != null)         {            foreach (var s in pro.Services)            {               var ss = (IBookService)s;               Console.WriteLine(ss.GetBookName());            }         }         foreach (var str in pro.InputString)         {            Console.WriteLine(str);         }         //調用無參數方法         if (pro.methodWithoutPara != null)         {            Console.WriteLine(pro.methodWithoutPara());         }         //調用有參數方法         if (pro.methodWithPara != null)         {            Console.WriteLine(pro.methodWithPara(3000));         }         Console.Read();      }            private void Compose()      {         var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());         CompositionContainer container = new CompositionContainer(catalog);         container.ComposeParts(this);      }   }}

匯入匯出方法用到了Func<T>委託,當然沒有傳回值的話可以用Action<T>委託,關於委託這裡就不多說了,大家可以自行百度。

點擊這裡下載源碼

C#可擴充編程之MEF學習筆記(三):匯出類的方法和屬性(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.