钻石组件对MEF的依赖性

时间:2018-12-09 19:19:20

标签: c# .net reflection mef

我有四个程序集,它们相互参照,如上图所示:

TpProjectModel共享接口,DILib和FileProject实现它们,而TpPtojektCLI根据配置文件中的设置使用其中一种实现方式

当我想在TpProjectCLI中编写它们时,我有以下例外情况:

  

组成保持不变。更改被拒绝,因为   出现以下错误:组合物产生了一个   组成错误。根本原因在下面提供。查看   CompositionException.Errors属性以获取更多详细信息。

     

1)找到多个与约束匹配的出口:     合同名称TpProjectModel.Reflection.IReflector     RequiredTypeIdentity TpProjectModel.Reflection.IReflector

     

结果:无法设置导入   'ViewModels.Dashboard.DashboardViewModel.Reflector   (ContractName =“ TpProjectModel.Reflection.IReflector”)'部分   'ViewModels.Dashboard.DashboardViewModel'。元件:   ViewModels.Dashboard.DashboardViewModel.Reflector   (ContractName =“ TpProjectModel.Reflection.IReflector”)->   ViewModels.Dashboard.DashboardViewModel

以及如何获取所有所需程序集的代码:

NameValueCollection plugins = (NameValueCollection)ConfigurationManager.GetSection("plugins");
string[] pluginsCatalogs = plugins.AllKeys;
List<DirectoryCatalog> assemblyCatalogs = new List<DirectoryCatalog>();

foreach (string pluginsCatalog in pluginsCatalogs)
{
    if (Directory.Exists(pluginsCatalog))
        assemblyCatalogs.Add(new DirectoryCatalog(pluginsCatalog));
}

AggregateCatalog catalog = new AggregateCatalog(assemblyCatalogs);
CompositionContainer container = new CompositionContainer(catalog);

try
{
    container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
    Console.WriteLine(compositionException.ToString());
    throw;
}
catch (Exception exception) when (exception is ReflectionTypeLoadException)
{
    ReflectionTypeLoadException typeLoadException = (ReflectionTypeLoadException)exception;
    Exception[] loaderExceptions = typeLoadException.LoaderExceptions;
    loaderExceptions.ToList().ForEach(ex => Console.WriteLine(ex.StackTrace));

    throw;
}

0 个答案:

没有答案