团结国际奥委会动态解析组件 [英] Unity IoC for resolving assemblies dynamically

查看:170
本文介绍了团结国际奥委会动态解析组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是统一为国际奥委会。我们遇到特殊的问题。
我们已经创建了名为接口IPlugin。这个接口是在各种第三方厂商共同开发基于此接口上自己的插件。这些插件然后安装到我们的系统中。
供应商将提供他们的插头作为DLL。我们要的是,
使用我们要解决它与IPlugin接口实现的所有组件的类型统一。我才知道,这是通过MEF导出属性,我想知道这是否可以通过统一使用一些简短的扩展来实现。



我们的代码

实现

 公共接口IPlugin 
{
虚空过程数据();
}

公共类数据处理器
{
无功pluginList = unityContainer.ResolveAssemblies< IPlugIn>()
/ *
没有这样的方法团结,但我们要的是扫描bin文件夹中的所有组件和加载它是由IPlugIn
* /
}


供应商的组件

 公共类AbcCompanyPlugIn:IPlugin 
{
虚空过程数据()
{
//一些代码
}

}
公共类XyzCompanyPlugIn:IPlugin
{
虚空过程数据()
{
//一些代码
}

}


解决方案

您可以写一点反射代码,扫描外接组件的文件夹,并注册所有 IPlugin 与容器的实现。



像这样的东西应该工作:

  VAR组件= //从T从磁盘读取
VAR pluginTypes =所有组件从组装在
在a.GetExportedTypes()
,其中的typeof(IPlugin)。 IsAssignableFrom(T)
选择吨;

的foreach(在pluginTypes变种T)
container.RegisterType(typeof运算(IPlugin),T);



(代码可能无法编译)


We are using unity as IoC. We came across unique problem. We have created interface called IPlugin. This interface is shared across various third party vendors to develop their own plug in based on this interface. These plug ins then fits into our system. Vendors will provide their plugs in as dll. What we want is , Using unity we want to resolve all assembly’s type which is implemented with IPlugin interface. I came to know that this is achievable via MEF export attribute, I am wondering whether this can be achieved via Unity using some short of extension.

Our code

Public interface IPlugin
{
    Void ProcessData();
} 

Public class DataProcessor
{
    Var pluginList = unityContainer.ResolveAssemblies<IPlugIn>()
/*
There is no such method in unity but what we want is scan all assemblies in bin folder and load all types which are inheriting from IPlugIn
*/
}

Vendor’s assembly

Public class AbcCompanyPlugIn : IPlugin
{
Void ProcessData()
{
// some code
}

}
Public class XyzCompanyPlugIn : IPlugin
{
Void ProcessData()
{
// some code
}

}

解决方案

You could write a bit of Reflection code that scans a folder for add-in assemblies and registers all IPlugin implementations with the container.

Something like this ought to work:

var assemblies = // read all assemblies from disk
var pluginTypes = from a in assemblies
                  from t in a.GetExportedTypes()
                  where typeof(IPlugin).IsAssignableFrom(t)
                  select t;

foreach (var t in pluginTypes)
    container.RegisterType(typeof(IPlugin), t);

(code may not compile)

这篇关于团结国际奥委会动态解析组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆