2009-04-23 39 views
1

我對AutoFac只有非常熟悉,並且我詢問下面詳細描述的功能是否可用,並且它看起來「不完全」。你知道這是可能的容器嗎?允許刪除註冊的.NET IOC容器

[TestFixture] 
public class SomeCunningTests 
{ 
    class SomeType  {  } 

    [Test] 
    public void TestRegistrationScope() 
    { 
     var container = new IocContainerOfSomeKind(); 
     using (new RegistrationScope(container, new SomeType())) 
     { 
      Assert.IsTrue(container.IsRegistered(typeof(SomeType))); 
     } 
     Assert.IsFalse(container.IsRegistered(typeof(SomeType))); 
    } 

    public class RegistrationScope<T> : IDisposable 
    { 
     public RegistrationScope(IocContainerOfSomeKind container, T instance) 
     { 
      //code here for registering instance 
     } 
     public void Dispose() 
     { 
      //unregister instance somehow 
     } 
    } 
} 

回答

1

MEF可以用於這種情況。我相信你所需要做的就是爲你的目錄使用一個AggregateCatalog,它提供了在運行時添加和從你的目錄中刪除的能力,因爲它公開了一個ComposablePartCatalogCollection

該目錄基本上是用於合成的類型的註冊表。