I need to implement a method which returns an object based on type like.
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;
}
}
Do i need to maintain dictionary like inside the GetObject method?
Dictionary<Type, IBase> test = new Dictionary<Type, IBase>();
Is there any better way for same?
[Edit]: – I dont want to create the object everytime. I need to keep it inside memory and when there is a call. I want to return the object from there. Apart from dictionary is there any other way?
1 Answer