mercredi 2 septembre 2015

Using Reflection to create automaticaly a List of Delegates

I have a Class called Operations.cs with some method. I want to create a List of Delegates to pick a method randomly. Right now I have the following working solution:

public delegate void Delmethod(ExampleClass supervisor);
public static Operations op = new Operations();
public List<Delmethod> opList = new List<Delmethod>();

opList.Add(op.OpOne);
opList.Add(op.OpTwo);
opList.Add(op.OpThree);
opList.Add(op.OpFour);
opList.Add(op.OpFive);
opList.Add(op.OpSix);
opList.Add(op.OpSeven);

But what I really want is generate the List opList automatically in the case I add a new method in the Operations.cs. I was trying to use reflection in an atempt to solve my problem as follows:

List<MethodInfo> listMethods = new List<MethodInfo>(op.GetType().GetMethods().ToList());

foreach (MethodInfo meth in listMethods)
{
   opList.Add(meth);
}

I believe that this doesn't work because I'm making a confusion about the meaning of delegate, but I'm out of ideas.





Aucun commentaire:

Enregistrer un commentaire