reference an array of methods

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've an unusual (?) question. I want to create an array of references to
methods having identical parameters and return types. I would like to
reference the appropriate method within the array via a enum type and then
execute the corresponding method. Is this possible?
 
Hi, Steve.
You can create an array of delegate with the same signature:
public delegate void Foo(int i);
Foo[] methodlist = ...;

Another way doing it is to use the invocation list built into Delegate
type, although this is not as straight forward as a simple delegate array.

Regards
Ming Chen
 
You might be able to use delegates here. Delegate exposes Method property
which basically is MethodInfo which has the "Name" which I assume is what
you would like use for your enum reference..
What I am saying is

class Test {
public delegate String Foo(int y);
public void TestDo(String name) {
Delagate[] arrDelegates = Foo.GetInvocationList();
foreach(Delagate d in arrDelegates) {
if (d.Method.Name.Equals(whatever you would like to be)) {
d(1);
break;
}
}
}
}
 
Thank you both. This helps a lot.

Girish Bharadwaj said:
You might be able to use delegates here. Delegate exposes Method property
which basically is MethodInfo which has the "Name" which I assume is what
you would like use for your enum reference..
What I am saying is

class Test {
public delegate String Foo(int y);
public void TestDo(String name) {
Delagate[] arrDelegates = Foo.GetInvocationList();
foreach(Delagate d in arrDelegates) {
if (d.Method.Name.Equals(whatever you would like to be)) {
d(1);
break;
}
}
}
}



--
Girish Bharadwaj
http://msmvps.com/gbvb
Steve Teeples said:
I've an unusual (?) question. I want to create an array of references to
methods having identical parameters and return types. I would like to
reference the appropriate method within the array via a enum type and then
execute the corresponding method. Is this possible?
 
Back
Top