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. Thinking in java学习笔记之String的不可变性

    为了提高效率,可以使用StringBuffer或StringBuilder 1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与 ...

  2. 向shell脚本中传入参数

    写一个 程序名为    test.sh    可带参数为 start 和 stop 执行  test.sh start执行  start 内容的代码 执行 test.sh stop 执行 stop 内 ...

  3. strlen()和sizeof()求数组长度

    在字符常量和字符串常量的博文里有提: 求字符串数组的长度 标准库函数strlen(s)可以返回字符串s的长度,在头文件<string.h>里. strlen(s)的判断长度的依据是(s[i ...

  4. ubuntu sudo update与upgrade的作用及区别

    ubuntu sudo update与upgrade的作用及区别 入门linux的同志,刚开始最迫切想知道的,大概一个是中文输入法,另一个就是怎么安装软件.本文主要讲一下LINUX安装软件方面的特点. ...

  5. SessionState

    SqlServer方式:1.创建数据库的方法:C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regsql -ssadd -sstype ...

  6. 使用JSF框架过程中的若干典型问题及其解决方案

    1.commandXxx点击后,不调用action中的方法: 原因1:xhtml后缀名的文件,最终也会转化为普通的html文件(这是熟悉JSF框架的关键.),commandXxx点击后不调用后台act ...

  7. 【荐】说说CSS Hack 和向后兼容

    人一旦习惯了某些东西就很难去改,以及各种各样的原因,新的浏览器越来越多,而老的总淘汰不了.增长总是快于消亡导致了浏览器兼容是成了谈不完的话题.说 到浏览器兼容,CSS HACK自然而然地被我们想起.今 ...

  8. Linux 下测试串口的命令microcom

    昨天应为要测试主板上的串口,查了一下,可以使用microcom 这条命令进行测试. 命令使用方法很简单: Usage: microcom [-d DELAY] [-t TIMEOUT] [-s SPE ...

  9. sql 创建表、删除表 增加字段 删除字段操作

    下面是Sql Server 和 Access 操作数据库结构的常用Sql,希望对你有所帮助. 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRI ...

  10. WebService -- Java 实现之 CXF (WebService 服务器端接口)

    1. 使用Maven创建一个quickstart项目 2. 引入依赖的Jar包 <dependency> <groupId>org.apache.cxf</groupId ...