Quiero crear una lista de métodos para ejecutar. Cada método tiene la misma firma. pensé en poner los delegados en una colección genérica, pero me siguen dando este error:¿Puedo utilizar una lista <T> como una colección de punteros de método? (C#)
'method' is a 'variable' but is used like a 'method'
En teoría, esto es lo que me gustaría hacer:
List<object> methodsToExecute;
int Add(int x, int y)
{ return x+y; }
int Subtract(int x, int y)
{ return x-y; }
delegate int BinaryOp(int x, int y);
methodsToExecute.add(new BinaryOp(add));
methodsToExecute.add(new BinaryOp(subtract));
foreach(object method in methodsToExecute)
{
method(1,2);
}
Cualquier ideas sobre cómo llevar a cabo ¿esta? Gracias!
No debería ser la última línea: methodsToExecute [0] (1,2); o incluso beeter: int n = methodsToExecute [0] (1,2); –