I am developing a C# application. I have this kind of generic method with a type parameter:
public abstract class StuffDoer
{
public abstract void DoStuff();
}
public class MyStuffDoer : StuffDoer
{
public override void DoStuff()
{
/* We do stuff here */
}
}
/* ... */
public void CreateAndDoStuff<T>() where T: StuffDoer, new()
{
T instance = new T();
instance.DoStuff();
}
public void MyCreateAndDoStuff()
{
CreateAndDoStuff<MyStuffDoer>();
}
However, I'd like to pass the type parameter in the actual method parameters so the type could be decided dynamically. In other words, I'd like to do something like this (example is not valid C# but should illustrate the point):
public void CreateAndDoStuff(Type t) where t: StuffDoer, new()
{
t instance = new t();
instance.DoStuff();
}
public void MyCreateAndDoStuff()
{
CreateAndDoStuff(typeof(MyStuffDoer));
}
Is this kind of thing somehow possible in C#?