all groups > dotnet clr > october 2004 >
You're in the

dotnet clr

group:

Delegate problem


Delegate problem Viorel Ghilas
10/5/2004 6:11:15 PM
dotnet clr:
Hi all,

I have a delegate like this public delegate void GetObject(Object source,
EventArgs e);
how can I take the parammeter types in runtime from this type, using
reflection. The problem is that I have this delegate, attribute class that
must be applied on the methods with delegate structure. Before invoke the
method I need to check if method correspond to delegate type. How to do it,
I am opening for any sugestions, pls.

with best regards
Viorel Ghilas

RE: Delegate problem Jakob Christensen
10/6/2004 4:49:12 AM
Hi,

When you declare a delegate a class is automatically generated for you
behind the scenes. This class has an Invoke method which takes the same
parameters as the declared delegate. This means that the following code will
print out the parameter-types for your GetObject delegate (which - by the way
- has the exact same signature as the delegate System.EventHandler provided
by the framework):

Type t = typeof(GetObject);
MemberInfo[] invoke = t.GetMember("Invoke");
ParameterInfo[] parameters = ((MethodInfo) invoke[0]).GetParameters();
foreach (ParameterInfo p in parameters)
Console.WriteLine(p.ParameterType.ToString());

HTH, Jakob.



[quoted text, click to view]
Re: Delegate problem Viorel Ghilas
10/6/2004 3:59:00 PM
Thanks, it's work very well.


[quoted text, click to view]

AddThis Social Bookmark Button