/// <summary>add handler for the .NET object: /// create a dynamic delegate according to the given event type /// add method body to the delegate /// in the method body call DefaultDotNetHandler which will handle the event</summary> /// <param name="eventName"> name of the event</param> /// <param name="objectToHook"> object to hook</param> /// <param name="objectEvents"> once registered, events are added to this collection</param> /// <param name="reportErrors"> indicates whether to log errors while hooking event. There are some /// default events that are hooked for each .NET control (see DNControlEvents._standardControlEvents). /// Errors occurring while hooking these events should not be reported.</param> /// TODO handle static events internal static void addHandler(String eventName, Object objectToHook, DNObjectEventsCollection.ObjectEvents objectEvents, bool reportErrors) { //subscribe for events only once , check if handler already added if (objectEvents.delegateExist(eventName)) { return; } Type type = objectToHook.GetType(); // Get an EventInfo representing the event, and get the type of delegate that handles the event. EventInfo eventInfo = type.GetEvent(eventName); if (eventInfo != null) { // Get the "add" accessor of the event and invoke it late-bound, passing in the delegate instance. // This is equivalent to using the += operator in C#, or AddHandler in Visual Basic. // The instance on which the "add" accessor is invoked is the form; // the arguments must be passed as an array. MethodInfo addHandler = eventInfo.GetAddMethod(); Delegate eventHandler = null; try { #if !PocketPC eventHandler = createDynamicDelegate(objectToHook.GetHashCode(), eventInfo); #else if (objectToHook is Control) { eventHandler = DNControlEvents.getPredefinedEvent(eventName); } #endif try { addHandler.Invoke(objectToHook, new Object[] { eventHandler }); } catch (Exception e) { if (e is TargetInvocationException && e.InnerException != null) { throw e.InnerException; } else { throw e; } } objectEvents.add(eventName, eventHandler); } catch (Exception exception) { if (reportErrors) { DNException dnException = Manager.GetCurrentRuntimeContext().DNException; dnException.set(exception); } } } else { if (reportErrors) { Events.WriteExceptionToLog(String.Format("Event type \"{0}\" not supported", eventName)); } } }
/// <summary> //This function registers "HandleDNControlValueChanged" to the event //specified by the user for DN control property change. /// </summary> /// <param name="eventName">Event to be registered</param> /// <param name="obj">object on which, event will be registered</param> internal static void AddDNControlValueChangedHandler(object objectToHook, String eventName) { Control ctrl = (Control)objectToHook; DNObjectEventsCollection.ObjectEvents objectEvents = DNManager.getInstance().DNObjectEventsCollection.checkAndCreateObjectEvents(ctrl); //DNControlValueChangedDelegate and DNControlValueChangedEventName should be initialized only once. If initialized second time, assert. //Debug.Assert(objectEvents.DNControlValueChangedDelegate == null && objectEvents.DNControlValueChangedEventName == null); Type type = ctrl.GetType(); //Get an EventInfo representing the event, and get the type of delegate that handles the event. EventInfo eventInfo = type.GetEvent(eventName); if (eventInfo != null) { // Get the "add" accessor of the event and invoke it late-bound, passing in the delegate instance. // This is equivalent to using the += operator in C#, or AddHandler in Visual Basic. // The instance on which the "add" accessor is invoked is the form; // the arguments must be passed as an array. MethodInfo addHandler = eventInfo.GetAddMethod(); Delegate eventHandler = null; try { if (objectEvents.DNControlValueChangedDelegate != null) { Debug.Assert(objectEvents.DNControlValueChangedEventName == eventName); eventHandler = objectEvents.DNControlValueChangedDelegate; } else { eventHandler = createDynamicDelegateForDNCtrlValueChangedEvent(ctrl.GetHashCode(), eventInfo); //Add delegate(event handler) to object events. objectEvents.DNControlValueChangedDelegate = eventHandler; //Add event name to object events. objectEvents.DNControlValueChangedEventName = eventName; } try { addHandler.Invoke(objectToHook, new Object[] { eventHandler }); } catch (Exception e) { if (e is TargetInvocationException && e.InnerException != null) { throw e.InnerException; } else { throw e; } } } catch (Exception exception) { DNException dnException = Manager.GetCurrentRuntimeContext().DNException; dnException.set(exception); } } else { Events.WriteExceptionToLog(String.Format("Event type \"{0}\" not supported", eventName)); } }