I'm only really familiar with AutoFac, and I asked if the feature detailed below was possible with it, and it seemed 'not exactly'. Do you know a container with which this would be possible?
[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
}
}
}