Since you are looping over instance methods you will need to create a
delegate with an invocation target. This means you can't pass the
MethodInfo to CreateDelegate, rather you will need to pass the instance
the method is to be invoked on and the name of the method. Also, I think
that you'll need to expand your reflection query to get some results,
you probably want something like this:

foreach (MethodInfo mi in
this.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
{

myArray.Add(Delegate.CreateDelegate(typeof(MyDelegate), this, mi.Name));

}

This assumes that all public methods on this class have the required
signature specified by MyDelegate.

John.

>-----Original Message-----
>From: Moderated discussion of advanced .NET topics. [mailto:ADVANCED-
>[EMAIL PROTECTED] On Behalf Of Griffiths, Ian
>Sent: Tuesday, 2 March 2004 10:36 PM
>To: [EMAIL PROTECTED]
>Subject: Re: [ADVANCED-DOTNET] Creating a delegate from reflection
>information
>
>You want the static method Delegate.CreateDelegate.
>
>Something like this:
>
>  myDelegate = (MyDelegate) Delegate.CreateDelegate(typeof(MyDelegate),
>mi);
>
>
>--
>Ian Griffiths - DevelopMentor
>http://www.interact-sw.co.uk/iangblog/
>
>> -----Original Message-----
>> From: Ryan Parlee
>>
>> I am wanting to create a delegate at runtime using reflection but am
>> unable
>> to get this working.  Specifically, I would like to something like
>this:
>>
>>
>> foreach (MethodInfo mi in
>> this.GetType().GetMethods(BindingFlags.Instance))
>> {
>>   myDelegate = new MyDelegate(mi.MethodHandle)
>>   myArray.Add(myDelegate);
>> }
>>
>>
>> The compiler complains that new MyDelegate() expects a method.  Is
>there a
>> way to do what I want to do?
>
>===================================
>This list is hosted by DevelopMentorR  http://www.develop.com
>Some .NET courses you may be interested in:
>
>NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
>http://www.develop.com/courses/gaspdotnetls
>
>View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to