I am trying to refactor some code by introducing generics, and I got stuck. I am trying to create a new instance of T, but the trouble is, that T has a delegate argument in the constructor. What I was aiming for was something like this:
public delegate IOrders DoStuffDelegate();
public class GenericBoss<T> where T:Worker
{
public void DelegateWork()
{
T worker = Activator.CreateInstance(typeof(T), new[]{GiveOrders})
worker.Work();
}
public IOrders GiveOrders()
{
return new OrderFromTheBoss();
}
}
public class Worker
{
private readonly DoStuffDelegate _takeOrders;
public Worker(DoStuffDelegate takeOrders)
{
_takeOrders = takeOrders;
}
public void Work()
{
_takeOrders();
}
}
However, this doesn't work, as only [object] types are allowed as arguments in the [Activator]. I am not allowed to change the constructor, so moving the delegate elsewhere is not possible.
Is there a way out, or is generics not an option here?
Regards, Morten