Microsoft KB Archive/327433

= HOW TO: Hook Up a Delegate to an Event by Using Reflection =

Article ID: 327433

Article Last Modified on 10/26/2002

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q327433



IN THIS TASK
SUMMARY
 * Get a Reference to the Type
 * Create an Instance of the Type
 * Load the System.Type Object
 * Create an Instance of the Delegate
 * Call the Method
 * Complete Code Sample



SUMMARY
This step-by-step article describes how to hook up a delegate by using reflection.

Typically, when you hook up a delegate to an event, you use the += syntax: myForm.OnFormSave += new Form1.FormSaveEventHandler(OnFormSave); However, you must directly reference the delegate name when you use this syntax. When you use reflection, you cannot directly reference the name, so you cannot use the += syntax.

back to the top

Get a Reference to the Type
Get a reference to the assembly that contains the class that you are interested in. In this case, the currently executing assembly contains the class that you want, so you call GetExecutingAssembly: Assembly tempAssembly = Assembly.GetExecutingAssembly; back to the top

Create an Instance of the Type
Create an instance of the class that has the delegate defined: Form thisForm = (Form)tempAssembly.CreateInstance(&quot;Tester.Form1&quot;, true, BindingFlags.CreateInstance, null, ConstructorArgs, _ null, null); The parameters are as follows:
 * string  - This is the name of the type to locate.
 * bool  - Set this to True to ignore case; otherwise, set it to False.
 * BindingFlags  - This is a bitmask that affects the way in which the search is conducted.
 * Binder  - This is an object that enables the binding, coercion of argument types, invocation of members, and retrieval of MemberInfo objects with reflection. If the binder is a null reference, the default binder is used.
 * object[]  - This is an array of type Object that contains the arguments to be passed to the constructor.
 * CultureInfo  - This is an instance of CultureInfo that is used to govern the coercion of types. If this is a null reference, the CultureInfo for the current thread is used.
 * object[]  - This is an array of type Object that contains one or more activation attributes that can participate in the activation.

back to the top

Load the System.Type Object
Load the System.Type object for the delegate that you want to add a method to. Notice that the plus sign (+) syntax that is used instead of the period (.): Type typeDelegate = tempAssembly.GetType(&quot;Tester.Form1+FormSaveEventHandler&quot;); back to the top

Create an Instance of the Delegate
Create an instance of the delegate. This is the same as the new Form1.FormSaveEventHandler in the &quot;Summary&quot; section: Delegate d = Delegate.CreateDelegate(typeDelegate, this, &quot;OnFormSave&quot;, false); The parameters are as follows:
 * Type  - This is the type of delegate to create.
 * object  - This is the class instance on which the method is invoked.
 * string  - This is the name of the instance method that the delegate is to represent.
 * bool  - This is a Boolean value that indicates whether to ignore the case when it compares the name of the method.

back to the top

Call the Method
Call the add method to hook up the delegate. The add_OnFormSaveData method is automatically generated when the delegate is defined. To call it, call InvokeMember: Object[] arr = {d}; Type typeForm1 = typeof (Form1); typeForm1.InvokeMember(&quot;add_OnFormSaveData&quot;, BindingFlags.InvokeMethod, null, thisForm, arr); The parameters are as follows:
 * string  - This is the string that contains the name of the constructor, method, property, or field member to invoke. You can also use an empty string (&quot;&quot;) to invoke the default member.
 * BindingFlags  - This is a bitmask that is made up of one or more BindingFlags that specify how the search is conducted.
 * Binder  - This is a Binder object that defines a set of properties and enables binding. It can also be a null reference (&quot;Nothing&quot; in Microsoft Visual Basic) if you want to use the DefaultBinder.
 * object  - This is the object on which to invoke the specified member.
 * object[]  - This is an array that contains the arguments to pass to the member to invoke.

back to the top

Complete Code Sample
The complete code is as follows for a Microsoft Visual C# .NET Windows Application project named Tester: Assembly tempAssembly = Assembly.GetExecutingAssembly;

// Create an instance of the object that has the delegate defined. Form thisForm = (Form)tempAssembly.CreateInstance(&quot;Tester.Form1&quot;, true, BindingFlags.CreateInstance, null, ConstructorArgs, _ null, null);

// Load the System.Type object for the delegate that you want to add a method to. Type typeDelegate = tempAssembly.GetType(&quot;Tester.Form1+FormSaveEventHandler&quot;); // Create an instance of the delegate. //This is the same as the &quot;new Form1.FormSaveEVentHandler&quot; above. Delegate d = Delegate.CreateDelegate(typeDelegate, this, &quot;OnFormSave&quot;, false);

// Call the add method to hook up the delegate. Object[] arr = {d}; Type typeForm1 = typeof (Form1); typeForm1.InvokeMember(&quot;add_OnFormSaveData&quot;, BindingFlags.InvokeMethod, null, thisForm, arr);

back to the top

Keywords: kbhowto kbhowtomaster KB327433

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.