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. Git从入门到学会

    Git简介 Git是什么? Git和SVN一样都是一种高效的管理代码的系统. Git是目前世界上最先进的分布式版本控制系统(没有之一). 创建版本库 什么是版本库呢?版本库又名仓库,英文名reposi ...

  2. <<< 数据库基础知识

    相关概念: 1.数据 (DATA) : 数据是描述现实世界事物的符号标记, 是指用物理符号记录下来的可以鉴别的信息. 包括:数字.文字.图形.声音.及其他特殊符号 2.数据库(DATABASE) :按 ...

  3. web开发——写一个简单的表格导出操作

    一.前台页面: 主要是一个按钮和一个表格,表格有显示数据,按钮负责将表格中的数据选择性地导出.除此外,可以附加一个小窗口和进度条,用于显示下载进度. 1. 按钮:<a href="ja ...

  4. 11月8日PHP练习《留言板》

    一.要求 二.示例页面 三.网页代码及网页显示 1.denglu.php  登录页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...

  5. JMS开发步骤和持久化/非持久化Topic消息

    ------------------------------------------------ 开发一个JMS的基本步骤如下: 1.创建一个JMS connection factory 2.通过co ...

  6. 关于war包 jar包 ear包 及打包方法

    关于war包 jar包 ear包 及打包方法 war包:是做好一个web应用后,通常是网站打成包部署到容器中 jar包:通常是开发的时候要引用的通用类,打成包便于存放管理. ear包:企业级应用 通常 ...

  7. nyoj 71 独木舟上的旅行(贪心专题)

    独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...

  8. 一段freemarker高级分页效果的代码

    <a onclick="page(1)">首页</a> [#if currpage != 1] [#assign last=currpage - 1] &l ...

  9. PAT Mooc datastructure 6-1

    Saving James Bond - Hard Version This time let us consider the situation in the movie "Live and ...

  10. Shell入门教程:Shell的基本结构

    shell程序的基本组成结构 shell结构大体是由设定变量.内置命令.shell的语法结构.函数组成. 使用实例说明:test.sh #!/bin/bash #说明使用/bin/bash作为这个脚本 ...