I want to instantiate any class in a common way. is it possible? I know this is crazy, but this is just a question waiting to be answered.
I tried this but it does not work.
public class blabla {
public void bla();
}
public class Foo<T>
{
Dictionary<string, Func<object>> factory;
public Foo()
{
factory = new Dictionary<string, Func<object>>();
}
public WrapMe(string key)
{
factory.Add(key, () => new T());
}
}
Foo<blabla> foo = new Foo<blabla>();
foo.Wrapme("myBlabla");
var instance = foo.factory["myBlabla"];
instance.Bla();
source
share