This is a similar technique to the one I use in controlinspector

http://www.codeproject.com/csharp/controlinspector.asp

Control-inspector hooks on to all Windows Forms events for a specific
project so you can see when events are firing.  It has a base class
which does the work, which has a method (this is from memory, so the
method names will probably be different in the code):

        void ReceiveEvent(object sender, object eventArgs);

I then use Reflection.Emit to make a derived class with the specific
delegate on it (ie:

        void ReceiveEventSpecialised(object sender, EventArgs e);

Or
        void ReceiveEventSpecialised(object sender, YourCustomEventArgs
e);

This method, of course, just calls the standard method in the base
class.  This restricts the amount of IL that needs to be written out
using Reflection.Emit.

I have to say, I remain indebted to Ian Griffiths for his help in coming
up with this on one of the developmentor courses, but it's quite an
interesting technique so I thought it was worth mentioning.

Best wishes
James 

> -----Original Message-----
> From: Moderated discussion of advanced .NET topics. 
> [mailto:[EMAIL PROTECTED] On Behalf Of 
> Eric Gunnerson
> Sent: 03 March 2004 01:22
> To: [EMAIL PROTECTED]
> Subject: Re: [ADVANCED-DOTNET] Creating a delegate from 
> reflection information
> 
> Weird. I have a column that covers that the just went live yesterday.
> 
> The problem you have is that when creating a delegate, you 
> need to have a type that matches it, but in the general case, 
> you don't.
> 
> I got around it by using reflection.emit to create the 
> appropriate type on the fly. This is an okay method if you're 
> going to call it a bunch, but if you only want to call it 
> once, InvokeMember() is a lot easier.
> 
> http://msdn.microsoft.com/library/default.asp?url=/library/en-
> us/dncscol
> /html/csharp02172004.asp
> 
> Hope that helps
> 
> Eric
> -----Original Message-----
> From: Moderated discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED] On Behalf Of Ryan Parlee
> Sent: Monday, March 01, 2004 10:39 PM
> To: [EMAIL PROTECTED]
> Subject: [ADVANCED-DOTNET] Creating a delegate from 
> reflection information
> 
> 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?
> 
> Thanks,
> Ryan
> 
> ===================================
> This list is hosted by DevelopMentor(r)  
> 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(r)  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