我有以下代码,并想为泛型的每个不同变体注册一个单例。这可能吗?目前,断言失败,因为它们不是相同的对象类型。
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));
}