HOW TO: Hook Up a Delegate to an Event by Using Reflection (327433)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition

This article was previously published under Q327433

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("Tester.Form1", true, BindingFlags.CreateInstance, null, ConstructorArgs, _
null, null); 
				
The parameters are as follows:
  • string typeName - This is the name of the type to locate.
  • bool ignoreCase - Set this to True to ignore case; otherwise, set it to False.
  • BindingFlags bindingAttr - This is a bitmask that affects the way in which the search is conducted.
  • Binder 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[] args - This is an array of type Object that contains the arguments to be passed to the constructor.
  • CultureInfo culture - 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[] activationAttributes - 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("Tester.Form1+FormSaveEventHandler");
				
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 "Summary" section:
 Delegate d = Delegate.CreateDelegate(typeDelegate, this, "OnFormSave", false);
				
The parameters are as follows:
  • Type type - This is the type of delegate to create.
  • object target - This is the class instance on which the method is invoked.
  • string method - This is the name of the instance method that the delegate is to represent.
  • bool ignoreCase - 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("add_OnFormSaveData", BindingFlags.InvokeMethod, null, thisForm, arr);		
				
The parameters are as follows:
  • string name - 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 ("") to invoke the default member.
  • BindingFlags invokeAttr - This is a bitmask that is made up of one or more BindingFlags that specify how the search is conducted.
  • Binder binder - This is a Binder object that defines a set of properties and enables binding. It can also be a null reference ("Nothing" in Microsoft Visual Basic) if you want to use the DefaultBinder.
  • object target - This is the object on which to invoke the specified member.
  • object[] args - 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("Tester.Form1", 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("Tester.Form1+FormSaveEventHandler");
				
// Create an instance of the delegate.
//This is the same as the "new Form1.FormSaveEVentHandler" above.
Delegate d = Delegate.CreateDelegate(typeDelegate, this, "OnFormSave", false);

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

				
back to the top

Modification Type:MajorLast Reviewed:10/26/2002
Keywords:kbhowto kbHOWTOmaster KB327433 kbAudDeveloper