3

我使用 CompositionBatch 将可组合部分添加到容器中,然后通过重新组合将其删除。一切都很好,组合和重组。但我的问题是底层对象没有被处置。我的代码如下所示:

[PartCreationPolicy(CreationPolicy.NonShared)]
[Export]
public class NonShared : IDisposable
{
    public NonShared()
    {
        Console.WriteLine("Constructor of NonShared");
    }

    public void Dispose()
    {
        Console.WriteLine("Disposing NonShared");
    }
}

class Program : IPartImportsSatisfiedNotification
{
    [Import(AllowDefault=true, AllowRecomposition=true)]
    private NonShared _nonShared;

    public void OnImportsSatisfied()
    {
        Console.WriteLine("Program.OnImportsSatisfied()");
    }

    static void Main()
    {
        new Program().Run();
    }

    private void Run()
    {
        var partDefinition = AttributedModelServices.CreatePartDefinition(typeof(NonShared), null);
        var exportingPart = partDefinition.CreatePart();

        var addingBatch = new CompositionBatch();
        addingBatch.AddPart(this);
        addingBatch.AddPart(exportingPart);

        var container = new CompositionContainer();
        container.Compose(addingBatch);

        // Do something.

        var removingBatch = new CompositionBatch();
        removingBatch.RemovePart(exportingPart);

        container.Compose(removingBatch);
    }
}

我希望调用 Nonshared.Dispose() ,但事实并非如此。正如AddPart/RemovePart 的部件生命周期所描述的,在这种情况下应该处理非共享部件。我的代码中有一些错误吗?

4

1 回答 1

1

据我所知,使用 CompositionBatch 添加的部分由 ComposablePartExportProvider 处理(这是一个实现细节),它不会处理它们。只有 CatalogPartExportProvider 会处理其导出的部件,并且要使用此提供程序,您需要让 MEF 创建这些部件。

有关这方面的更多信息,您可以阅读weshaggardReleaseExport 真正做什么?.

如果您使用 .NET 4.5,则可以使用MEF2 引入的约定模型。否则,您可以使用TypeCatalogAggregateCatalog组合向容器添加类型,CompositionContainer.ReleaseExport随意释放(并在支持时处置)非共享部分和 AggregateCatalog.Catalogs。移除以从容器中移除类型。

这是使用 a 的示例TypeCatalog

class Program : IPartImportsSatisfiedNotification
{
    [Import(AllowDefault=true, AllowRecomposition=true)]
    private Lazy<NonShared>_nonShared; //Lazy<T> is needed for ReleaseExport to work.

    public void OnImportsSatisfied()
    {
        Console.WriteLine("Program.OnImportsSatisfied()");
    }

    static void Main()
    {
        new Program().Run();
    }

    private void Run()
    {
        var aggregateCatalog = new AggregateCatalog();
        using (var container = new CompositionContainer(aggregateCatalog ))
        {
            container.ComposeParts(this);
            //Check if the field is injected. It shouldn't be since the 
            //NonShared type is not known to the container yet..
            Console.WriteLine("NonShared field {0}", this._nonShared != null ? "exists" : "does not exist");
            //Add the NonShared type to a type catalog.
            var typeCatalog = new TypeCatalog(typeof(NonShared));
            //Add the TypeCatalog to the AggregateCatalog.
            aggregateCatalog.Catalogs.Add(typeCatalog);
            //Check if the field is injected. This time it should be.
            Console.WriteLine("NonShared field {0}", this._nonShared != null ? "exists" : "does not exist");

            if(this._nonShared != null)
            {
                //Access the lazy object so it gets a value.
                this._nonShared.Value.ToString();
                //Release the part. The Dispose method should be called.
                container.ReleaseExport<NonShared>(this._nonShared);
            }
        }
    }
}

您可以使用以下方法轻松测试整个类型目录的删除:

aggregateCatalog.Catalogs.Remove(typeCatalog);
于 2013-08-20T15:09:05.610 回答