silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了MEF应用(4) 组合容器 目录大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

组合容器   Castle有容器,Unity有容器,同样作为一个能够提供扩展机制,能够支持依赖注入的框架肯定也有容器。MEF的组合模型的核心是组合容器,该容器包含所有可用的部件并执行组合操作(即,将导入和导出配对) 。通常我们使用的组合容器是:CompositionContainer ,MEF还提供一个组合对象:CompositionBatch。   目录   前面我们有谈到组合容器中包含所有可用

组合容器

  Castle有容器,Unity有容器,同样作为一个能够提供扩展机制,能够支持依赖注入的框架肯定也有容器。MEF的组合模型的核心是组合容器,该容器包含所有可用的部件并执行组合操作(即,将导入和导出配对) 。通常我们使用的组合容器是:CompositionContainer ,MEF还提供一个组合对象:CompositionBatch。

 

目录

  前面我们有谈到组合容器中包含所有可用部件,并对这些组件执行组合操作,我们可能会问容器怎么发现这些部件呢?答案就是:目录(Catalog)。

@H_931_20@mEF中提供的目录对象主要有:Assembly Catalog(程序集目录),Directory Catalog,Aggregate Catalog,Type Catalog,和仅使用在Silverlight中得目录Deployment Catalog( Silverlight only),Filtered Catalog.

1.Assembly Catalog

可以在给定的Assembly 发现所有的导出部件,使用类型AssemblyCatalog

var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecuTingAssembly());

2.Directory Catalog

它可以在给定的目录(路径,相对路径或绝对路径)中发现导出部件,使用类型DirectoryCatalog。如果你使用的是相对路径,则相对的是当前AppDoamin的基路径。DirectoryCatalog只会对给定目录进行一次性扫描,目录发生变化是容器不会主动刷新,如果需要刷新给定的目录需要调用方法refresh() ,当目录刷新时,容器也会重新组合部件。

var catalog = new DirectoryCatalog("Extensions");
catalog.refresh();

3.Aggregate Catalog

聚集目录,有时候我们使用单一的Assembly Catalog和Directory Catalog并不能解决我们的需求,我们可能需要同时使用到他们,这时候我们便可使用Aggregate Catalog,我们可以将Assembly Catalog和Directory Catalog同时添加到目录中,这种添加可以通过构造函数实现,也可以通过目录集合的添加方法来实现:catalog.Catalogs.Add(...)。聚集目录使用类型AggregateCatalog

var catalog = new AggregateCatalog(
  new AssemblyCatalog(System.Reflection.Assembly.GetExecuTingAssembly()),new DirectoryCatalog("Extensions"));

4.Type Catalog

通过Type Catalog我们可以发现指定类型中得导出部件。使用类型TypeCatalog

var catalog = new TypeCatalog(typeof(type1),typeof(type2),...);

5.Deployment Catalog
Deployment Catalog,这种类型的目录仅支持Silverlight在后面的文章中我们会专门讲到如何在silverlight中使用MEF。

 

6.Filtered Catalog

已过滤的目录,通过FilteredCatalog可以筛选出特定的目录,特别是,您可以请求所有可加载的插件都有一个指示级别的元数据属性

 

 

var catalog = new AssemblyCatalog(typeof(Program).Assembly);
 var parent = new CompositionContainer(catalog);
 
 var filteredCat = new FilteredCatalog(catalog,def => def.Metadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadatanamE) &&
     ((CreationPolicy)def.Metadata[CompositionConstants.PartCreationPolicyMetadataname]) == CreationPolicy.NonShared);
 var child = new CompositionContainer(filteredCat,parent);
 
 var root = child.GetExportedObject<Root>();
 child.Dispose();


 

View Code 
 class Program    
 {       
 //导入,使用认的ContractType和ContractName 
        [Import]
         public ILog log
         { 
            get;
             set; 
        }
 
          static void Main(String[] args)
         {
             Program pro = new Program();
             pro.Compose();
             pro.log.AddLog(new Exception("My First MEF"));
             Console.Read();
         }
 
         //组合方法
         private void Compose()
         {
             //指定目录为当前执行的程序集
             var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecuTingAssembly());
            //使用AssemblyCatalog创建组合容器
             var container = new CompositionContainer(catalog);
 
            //调用组合部件方法 
            container.ComposeParts(this);
         }
      }
     //定义日志接口
     public interface ILog
     {
         void AddLog(Exception eX);
     }
 
     //导出部件,指定协定类型(ContractTypE)为ILog
     [Export(typeof(ILog))]
     public class MyLog : ILog
     {
         public void AddLog(Exception eX)
         {
             Console.WriteLine(ex.ToString());
         }
     }


 

 

we create a catalog -- that tells MEF where to look for imports and exports

we create a Composition container-- this is effectively the soup that all the different parts will be wired up together.

大佬总结

以上是大佬教程为你收集整理的MEF应用(4) 组合容器 目录全部内容,希望文章能够帮你解决MEF应用(4) 组合容器 目录所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。