我需要实现一个方法,该方法返回一个基于类型的对象。
public interface IBase
{
}
public class Base1 : IBase { }
public class Base2 : IBase { }
public class Base3 : IBase { }
public class MyClass
{
public IBase GetObject<T>() where T:IBase
{
// should return the object based on type.
return null;
}
}
我需要像在 GetObject 方法中那样维护字典吗?
Dictionary<Type, IBase> test = new Dictionary<Type, IBase>();
有没有更好的方法?
[编辑]: - 我不想每次都创建对象。我需要将它保存在内存中以及有电话时。我想从那里返回对象。除了字典还有其他方法吗?