2013-08-15 1 views
2

저는 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의 Parts Lifetime에 설명되어 있듯이 비공유 파트는이 상황에서 처리해야합니다. 내 코드에 실수가 있습니까?

답변

0

내가 아는 한 CompositionBatch를 사용하여 추가 한 부품은 해당 부품을 처리하지 않는 ComposablePartExportProvider (구현 세부 사항)에 의해 처리됩니다. CatalogPartExportProvider 만 내 보낸 파트를 처리하고이 공급자를 사용하려면 MEF가 파트를 작성하도록해야합니다.

자세한 내용은 weshaggard (What does ReleaseExport really do?)을 참조하십시오.

.NET 4.5에서 작업하는 경우 MEF2와 함께 도입 된 규약 모델을 사용할 수 있습니다. TypeCatalogAggregateCatalog과 결합하여 유형을 컨테이너에 추가하고 CompositionContainer.ReleaseExport을 사용하여 비공유 부품을 임의로 해제하고 (지원되는 경우 처분) AggregateCatalog.Catalogs를 사용할 수 있습니다. 컨테이너에서 유형을 제거하려면 제거하십시오.

aggregateCatalog.Catalogs.Remove(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); 
      } 
     } 
    } 
} 

쉽게와 전체 유형 카탈로그의 제거를 테스트 할 수 있습니다

는 여기 TypeCatalog을 사용하여 샘플입니다

관련 문제