MEF学习笔记

MEF是 Managed Extensibility Framework简称,在计算机的世界什么都会加一个简称,这我们大家已经司空见惯了。从名字我们可以知道它是一个用于管理的可扩展性框架。这是和EL不同的另一种IOC方式;

   MEF 为我们提供了一种运行时的扩展,具体应用在对象的实例化。它有目录(AssemblyCatalog)和容器(CompositionContainer)组成,他有输入输出(Exports和Imports)元数据标记,在对象实例化的时候会自动匹配这个契约。

下面是官方的一个展示图:

由CompositionContainer来管理我们的多个可组合部件。

下面我们来一个简单的示例来初步了解MEF:

控制台程序:

 
 
  1. 代码   
  2.  
  3. using System;   
  4. using System.Collections.Generic;   
  5. using System.Linq;   
  6. using System.Text;   
  7. using System.ComponentModel.Composition;//添加引用   
  8.     
  9.  
  10.  
  11. namespace MEFTest   
  12. {   
  13.     public interface ILog   
  14.     {   
  15.         void Write(string message);   
  16.     }   
  17.       
  18.     [System.ComponentModel.Composition.Export(typeof(ILog))]   
  19.     public class ConsoleLog:ILog   
  20.     {   
  21.         public void Write(string message)   
  22.         {   
  23.             Console.WriteLine("Hello:"+message);   
  24.         }   
  25.     }   
  26.     class Program   
  27.     {   
  28.         [System.ComponentModel.Composition.Import(typeof(ILog))]   
  29.         public ILog Log;   
  30.         static void Main(string[] args)   
  31.         {   
  32.             Program pro = new Program();   
  33.             pro.Init();   
  34.             pro.Log.Write("Test");   
  35.             Console.Read();   
  36.         }  
  37.  
  38.         public void Init()   
  39.         {   
  40.             var assem = System.Reflection.Assembly.GetExecutingAssembly();//当前程序集   
  41.             var assemcat = new System.ComponentModel.Composition.Hosting.AssemblyCatalog(assem);//构建目录   
  42.             var container = new System.ComponentModel.Composition.Hosting.CompositionContainer(assemcat);//构建容器   
  43.             container.ComposeParts(this);//这是一个System.ComponentModel.Composition.AttributedModelServices 的扩展方法。   
  44.         }   
  45.     }   
  46. }   
  47.  

输出为:

Hello:Test 
 

ExportAttribute 定义如下:

 
 
  1.  代码   
  2.  
  3. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = false)]   
  4. public class ExportAttribute : Attribute   
  5. {   
  6.       
  7.     [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]   
  8.      
  9.     [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]   
  10.      
  11.     [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]   
  12.     public ExportAttribute(Type contractType);   
  13.      
  14.     public ExportAttribute(string contractName, Type contractType);  
  15.  
  16.       
  17.     public string ContractName { get; }   
  18.       
  19.     public Type ContractType { get; }   
  20. }   
  21.  

从他的定义我们可以知道他可以运用于我们的类、方法、属性、字段,并且可以多继承,但是不存在从父类的继承特性。

他有三种不同的标记方式方式.在MEF中这里标记为可以导出,即注入到Import。

ImportAttribute 定义:

 
 
  1. 代码   
  2.  
  3. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]   
  4.     public class ImportAttribute : Attribute, IAttributedImport   
  5.     {   
  6.          
  7.         [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]   
  8.         public ImportAttribute();   
  9.          
  10.         [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]   
  11.         public ImportAttribute(string contractName);   
  12.          
  13.         [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]   
  14.         public ImportAttribute(Type contractType);   
  15.          
  16.         public ImportAttribute(string contractName, Type contractType);   
  17.          
  18.         public bool AllowDefault { get; set; }  
  19.  
  20.         public bool AllowRecomposition { get; set; }   
  21.           
  22.         public string ContractName { get; }   
  23.           
  24.         public Type ContractType { get; }   
  25.           
  26.         public CreationPolicy RequiredCreationPolicy { get; set; }   
  27.     }  
  28.  

 同上Export,这里标记为可被Export注入的接口点。

今天就写到这里了,希望大家多多指教,大家一起共同进步。






 本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/834786,如需转载请自行联系原作者


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值