1

我有两个 Exports 类IScreen,但是当我使用导入相同的类时

[ImportMany(typeof(IScreen))]
private IEnumerable<Lazy<IScreen,IJIMSMetadata>> _modules;
public IEnumerable<Lazy<IScreen, IJIMSMetadata>> Modules
{
        get { return _modules; }
}

模块包含四个 IScreen 实例,但我只导出了两个。

这是容器

container = new CompositionContainer(
                new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)))                
                );


protected override IEnumerable<Assembly> SelectAssemblies()
{
    string _modulePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Modules");
    var files = Directory.GetFiles(_modulePath, "*.dll", SearchOption.AllDirectories);

    List<Assembly> assemblies = new List<Assembly>();
    assemblies.Add(Assembly.GetExecutingAssembly());
    foreach (var file in files)
    {
        assemblies.Add(Assembly.LoadFrom(file));
    }
    return assemblies;
}
4

1 回答 1

2

由于您使用的是 AggregateCatalog,因此请检查以确保您没有为包含正在执行的程序集的位置添加 AssemblyCatalog 和 DirectoryCatalog。

例如,以下代码避免了两次处理相同的程序集。

var catalog = new AggregateCatalog();
var locations = new List<string>();
foreach (var loc in GetPluginDirectories())
    if (!locations.Contains(loc))
    {
        catalog.Catalogs.Add(new DirectoryCatalog(loc));
        locations.Add(loc);
    }

var asm = Assembly.GetExecutingAssembly();
if (!locations.Contains(Path.GetDirectoryName(asm.Location)))
    catalog.Catalogs.Add(new AssemblyCatalog(asm));
于 2013-10-14T20:29:22.343 回答