namespace Microshaoft.MEF.Contracts
{
using System;
public delegate void ExceptionEventHandler<TSender>(TSender sender, Exception exception);
}
namespace Microshaoft.MEF.Contracts
{
using System;
public interface IMefChainedProcessorPart<TContainer, TPartKey, TResult, TParameter>
{
IMefChainedProcessorPart<TContainer, TPartKey, TResult, TParameter> Instance
{
get;
}
int Priority
{
get;
}
TPartKey Key
{
get;
}
void OnOnceProcessAction(params TParameter[] parameters);
TResult OnOnceProcessFunc(params TParameter[] parameters);
void OnChainedOnceProcessAction(out ChainedProcessNextStep next, params TParameter[] parameters);
TResult OnChainedOnceProcessFunc(out ChainedProcessNextStep next, params TParameter[] parameters);
void OnChainedOnceAsyncQueueProcessAction(out ChainedProcessNextStep next, params TParameter[] parameters);
bool OnChainedOnceAsyncQueueProcessFunc(out ChainedProcessNextStep next, params TParameter[] parameters);
event ExceptionEventHandler<IMefChainedProcessorPart<TContainer, TPartKey ,TResult, TParameter>> OnCaughtExceptionInContainer;
string GetRuntimeTypeFullName();
Type GetRuntimeType();
}
public enum ChainedProcessNextStep
{
Continue
, Break
}
}
namespace Microshaoft.MEF.Contracts
{
using System;
using System.Xml;
public interface IMefPartsCompositionContainer<TPart, TPartKey, TResult, TInvokeParameter>
{
TPart[] Parts
{
get;
}
void ImportManyExports(string path);
void ChainedInvokeAllPartsProcessAction(params TInvokeParameter[] parameters);
TResult ChainedInvokeAllPartsProcessFunc(params TInvokeParameter[] parameters);
TResult InvokeOnePartProcessFunc(TPartKey PartKey, params TInvokeParameter[] parameters);
void InvokeOnePartProcessAction(TPartKey PartKey, params TInvokeParameter[] parameters);
}
}
//=============================================================================================================================
namespace Microshaoft.MEF.CompositionContainers
{
using Microshaoft;
using Microshaoft.MEF.Contracts;
using System;
using System.Collections.Concurrent;
using System.ComponentModel.Composition;
using System.Linq;
public class XmlMessageProcessorsCompositionContainer
: IMefPartsCompositionContainer
<
IMefChainedProcessorPart
<
XmlMessageProcessorsCompositionContainer
, string
, string
, string
>
, string
, string
, string
>
{
[ImportMany(typeof(IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string>))]
public IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string>[] Parts
{
get;
private set;
}
ConcurrentDictionary<string, IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string>> _dictionary;
public void ImportManyExports(string path)
{
MEFHelper.ImportManyExportsComposeParts<XmlMessageProcessorsCompositionContainer>
(
path
, this
);
var result = Parts.OrderBy(x => x.Priority);
if (_dictionary == null)
{
_dictionary = new ConcurrentDictionary<string, IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string>>();
}
result.ToList().ForEach
(
x
=>
{
_dictionary[x.Key] = x;
}
);
}
public void ChainedInvokeAllPartsProcessAction(params string[] parameters)
{
throw new NotImplementedException();
}
public string ChainedInvokeAllPartsProcessFunc(params string[] parameters)
{
throw new NotImplementedException();
}
public string InvokeOnePartProcessFunc(string PartKey, params string[] parameters)
{
string r = string.Empty;
IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer,string, string, string> part;
if (_dictionary.TryGetValue(PartKey, out part))
{
r = part.OnOnceProcessFunc(parameters[0]);
}
return r;
}
public void InvokeOnePartProcessAction(string PartKey, params string[] parameters)
{
throw new NotImplementedException();
}
}
}
namespace Microshaoft.MEF.CompositionContainersManagers
{
using Microshaoft.MEF.CompositionContainers;
public static class CompositionContainersManager
{
private static XmlMessageProcessorsCompositionContainer _xmlMessageProcessorsCompositionContainer =
new XmlMessageProcessorsCompositionContainer();
public static XmlMessageProcessorsCompositionContainer xmlMessageProcessorsCompositionContainer
{
get { return CompositionContainersManager._xmlMessageProcessorsCompositionContainer; }
set { CompositionContainersManager._xmlMessageProcessorsCompositionContainer = value; }
}
}
}
namespace Microshaoft
{
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
public static class MEFHelper
{
public static void ImportManyExportsComposeParts<T>(string path, T attributedPart)
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(path));
var container = new CompositionContainer(catalog);
container.ComposeParts(attributedPart);
}
}
}
//=====================================================================================================================
//可扩展部件样例
namespace Microshaoft.MEF.Parts
{
using Microshaoft.MEF.Contracts;
using Microshaoft.MEF.CompositionContainers;
using System;
using System.ComponentModel.Composition;
[Export(typeof(IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string>))]
public class SampleXmlMessageProcessorPart : IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string>
{
public IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string> Instance
{
get { throw new NotImplementedException(); }
}
public int Priority
{
get
{
return 100;
}
}
public string Key
{
get
{
return "SampleXmlMessageProcessorPart";
}
}
public void OnOnceProcessAction(params string[] parameters)
{
throw new NotImplementedException();
}
public string OnOnceProcessFunc(params string[] parameters)
{
return "";
}
public void OnChainedOnceProcessAction(out ChainedProcessNextStep next, params string[] parameters)
{
throw new NotImplementedException();
}
public string OnChainedOnceProcessFunc(out ChainedProcessNextStep next, params string[] parameters)
{
throw new NotImplementedException();
}
public void OnChainedOnceAsyncQueueProcessAction(out ChainedProcessNextStep next, params string[] parameters)
{
throw new NotImplementedException();
}
public bool OnChainedOnceAsyncQueueProcessFunc(out ChainedProcessNextStep next, params string[] parameters)
{
throw new NotImplementedException();
}
public string GetRuntimeTypeFullName()
{
throw new NotImplementedException();
}
public Type GetRuntimeType()
{
throw new NotImplementedException();
}
void SampleXmlMessageProcessorPart_OnCaughtExceptionInContainer(IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string> sender, Exception exception)
{
throw new NotImplementedException();
}
public event ExceptionEventHandler<IMefChainedProcessorPart<XmlMessageProcessorsCompositionContainer, string, string, string>>
OnCaughtExceptionInContainer =
(
(x, y) =>
{
}
);
}
}

MEF Parts Sample的更多相关文章

  1. MEF and AppDomain z

    MEF and AppDomain - Remove Assemblies On The Fly This article will give an idea of what's involved i ...

  2. MEF引起的内存泄露

    也许你编程的时候很小心,注意不引起内存泄露,例如不要被全局Static的变量引用上,注意Singleton的static引用,注意Event Handler注销,注意IDisposable接口实现,而 ...

  3. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  4. UVA 507 - Jill Rides Again 动态规划

      Jill Rides Again  Jill likes to ride her bicycle, but since the pretty city of Greenhills where sh ...

  5. Network POJ - 3417(LCA+dfs)

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

  6. poj3417Network【LCA】【树形DP】

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

  7. poj3417 Network 树上差分+LCA

    题目传送门 题目大意:给出一棵树,再给出m条非树边,先割掉一条树边,再割掉一条非树边,问有几种割法,使图变成两部分. 思路:每一条 非树边会和一部分的树边形成一个环,分三种情况: 对于那些没有形成环的 ...

  8. POJ3417Network

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5311   Accepted: 1523 Descripti ...

  9. POJ3417Network(LCA+树上查分||树剖+线段树)

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

随机推荐

  1. USB Keyboard Recorder

    catalogue . 引言 . Device Class Definition for Human Interface Devices (HID) . USB HID Report Descript ...

  2. linux之svn

    sudo apt-get install subversion 实践出真理

  3. 动手实验iptables的NAT功能实现流量穿透

    1.NAT和iptables理论见: http://lustlost.blog.51cto.com/2600869/943110 2.引子 近期,有同事抱怨说数据入库时,由于数据库所在的服务器只有内网 ...

  4. POJ3249:Test for Job

    传送门 很简单的一道题,被卡了几次,死于答案非法统计. 题意是求图里的一条最长的路径满足起点的入度和终点的出度都是0,而且图是DAG. 既然是DAG求最长路,DP即可.搞出拓扑序,逆序DP,然后统计所 ...

  5. 【转载】Python与ArcGIS Engine的集成

    本文转载自Fransico<Python与ArcGIS Engine的集成>   1 在Python中调用AO类库 1.1  准备工作 本文所使用环境:ArcGIS 10.0.Python ...

  6. ecshop后台 计划任务

    计划任务定时清理掉设置后的内容 主要针对单表删除(日志,):对于多表删除,不太好用(订单+订单商品+订单日志) 结构: 1.计划任务语言包:languages\zh_cn\cron\ 2.php文件: ...

  7. linux集群时钟问题

    一.ntpd与ntpdate的区别: 摘自:ntpd与ntpdate的区别 - 百事乐 - 博客园  http://www.cnblogs.com/liuyou/archive/2012/07/29/ ...

  8. 进入OS前的两步之System tick

    OK,继续向操作系统迈进.由简入繁,先实现两个小功能.第一个是system tick,第二个是任务切换(PendSV).一个是操作系统的心跳,一个是操作系统的并发处理的具体实现. System tic ...

  9. VTK初学一,vtkDelaunay2D创建球冠曲面

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  10. 【译文】Java Logging

    本文讲Java内置的java.util.logging软件包中的 api.主要解释怎样使用该api添加logging到你的application中,怎样加配置它等.但是本文不谈你应该把什么东西写到日志 ...