这与我的代码看起来类似
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
_container = new CompositionContainer(catalog);
Run Code Online (Sandbox Code Playgroud)
代码分析显示警告CA2000:在对所有引用超出范围之前调用目录上的Dispose.
所以我不确定是否需要禁止警告或将_catalog转换为字段+处理它.
MEF Docs似乎没有提到这一点.
根据MEF Preview 9源代码(可能与 .NET 4 中提供的代码非常匹配)CompositionContainer会将目录包装在CatalogExportProvider. 该导出提供程序存储在字段中并与容器一起处置。但是,CatalogExportProvider.Dispose不会依次处理包装好的ComposablePartCatalog.
因此答案是否定的:CompositionContainer不配置目录。
您可以通过运行以下代码来验证这一点,该代码不会在控制台上打印任何内容:
class MyCatalog : ComposablePartCatalog
{
protected override void Dispose(bool disposing)
{
Console.WriteLine("Disposed!");
base.Dispose();
}
public override IQueryable<ComposablePartDefinition> Parts
{
get { throw new NotImplementedException(); }
}
}
class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer(new MyCatalog());
container.Dispose();
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)