help with array of functions

  • Thread starter Thread starter Guest
  • Start date Start date
If you are attempting to use different functions withint he same array then:

1) Declare delegate types
2) declare array of delegate[] type - use: (Delegate[] abc = new
Delegate[n];) not to be confused with (delegate[] abc = new Delegate[n];)
3) i

delegate void testdelegate(int param);

class tclass
{
public void testfunction(int p)
{
}
}

Delegate[] abc = new Delegate[10];
for (int op_counter = 0; op_counter < abc.Length; op_counter++)
{
if (abc[op_counter] == null) continue;
// if you know the delegate type then just use a cast
((testdelegate)abc[op_counter])(0);
// if you dont know the type, then ether query or call
invoke and catch an error
abc[op_counter].DynamicInvoke(new object[] { op_counter });
// validate delegate is actually pointing to the method you
think it should be poiting to
// use reflection for that - which parameters you are
comparing is up to you

//(typeof(tclass).GetMethod("testfunctionname");
// abc[op_counter].Method <-- will give you method info just
as above

}


be careful with exceptions. Also remember, that dynamic invoke is quite slow
comparing to statically defined types. Remmeber that dynamicinvoke takes
object[] as a paramter, therefore any integer values will get boxed/unboxed
(read in - performance issues).

On a bright note, if implemented properly it works very well. I have
implemented expression parser this way...

- Arthur
 
Back
Top