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. java 中的Scanner

    java.util.Scanner是Java5的新特征,主要功能是简化文本扫描.这个类最实用的地方表现在获取控制台输入,其他的功能都很鸡肋,尽管Java API文档中列举了大量的API方法,但是都不怎 ...

  2. 在WildFly中运行多个standalone模式的实例

      WildFly作为一款优秀的EJB容器,其前身为JBoss AS.JBoss作为一款开源的应用服务器,被广泛的应用在各种项目当中.假设我们现在有这样一个项目,他是以standalone的模式运行在 ...

  3. HTTP协议—— 简单认识TCP/IP协议

    大学没读计算机专业,所以很多的专业知识都不知道.既然已经从事了IT这个行业,就势必要去了解下网络底层,虽然实际工作中这些东西用不到.高楼大厦,起于平川.不积跬步,无以至千里,不积小流,无以成江海.我现 ...

  4. elk系列8之logstash+redis+es的架构来收集apache的日志

    preface logstash--> redis --> logstash --> es这套架构在讲究松耦合关系里面是最简单的, 架构图如下: 解释下这个架构图的流程 首先前端lo ...

  5. C#之字符串篇

    大杂烩 一.类型转换    字符串转整形: int a = int.Parse(""); //不能转换null int b = Convert.ToInt32("&quo ...

  6. ASP.NET中使用JqGrid完整实现

    文章提纲 介绍 & 使用场景 JqGrid的一些说明 JqGrid和ASP.NET整合详细步骤 前置准备 框架搭建 数据填充 数据增/删/改 其他 介绍&使用场景 JqGrid不是一个 ...

  7. C++内存动态分配

    https://www.percona.com/blog/2012/07/05/impact-of-memory-allocators-on-mysql-performance/ https://su ...

  8. js 时间相关函数

    实例: <!doctype html> <html> <head> <meta charset="utf-8"> <title ...

  9. python requests模块使用

    python的网络编程能力十分强大,其中python中的requests库宣言:HTTP for Humans (给人用的 HTTP 库) 在网络编程中,最基本的任务包含: 发送请求 登录 获取数据 ...

  10. 图解TCP、IP笔记

    七层:应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 应用层:与通信无关的功能 表示层:例如转换编码格式 会话层:采用哪种连接方法 传输层以下: 传输层:确立连接与断开连接重发 网络层:从 ...