I have the below code, and would like to register a singleton for each different variant of the generic. Is this possible? Currently, the assertion fails, as the are not the same object type.
public interface IGenericClass<T>
{
string GetToString();
}
public class GenericClass<T> : IGenericClass<T>
{
public string GetToString()
{
return typeof (T).FullName;
}
}
[Test]
public void test()
{
var container = new Container();
container.RegisterOpenGeneric(
typeof(IGenericClass<>),
typeof(GenericClass<>));
var instance1 = container.GetInstance<IGenericClass<double>>();
var instance2 = container.GetInstance<IGenericClass<double>>();
//this should assert true
Assert.IsTrue(object.ReferenceEquals(instance1, instance2));
}
Just use RegisterSingleOpenGeneric
:
_container.RegisterSingleOpenGeneric(
typeof(IGenericClass<>),
typeof(GenericClass<>));