2009-04-23 37 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

该目录基本上是用于合成的类型的注册表。