Created
June 6, 2013 14:33
-
-
Save vendettamit/5721958 to your computer and use it in GitHub Desktop.
Check if the event is hooked with the Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Usage() | |
{ | |
var instance = new Timer(); | |
// check if the event is assigned a handler | |
if (GetEventHandler(instance, "Elapsed", "ElapsedEventHandler") == null) | |
{ | |
throw new InvalidOperationException(string.Format("The Timer elapsed event must be assigned an event handler. Source: {0}", type.FullName)); | |
} | |
} | |
/// <summary> | |
/// Gets the event handler. | |
/// </summary> | |
/// <param name="classInstance">The class instance.</param> | |
/// <param name="eventName">Name of the event.</param> | |
/// <returns></returns> | |
static ElapsedEventHandler GetEventHandler(Timer classInstance, string eventName, string eventHandler) | |
{ | |
ElapsedEventHandler eventDelegate = null; | |
Type classType = classInstance.GetType(); | |
var eventField = classType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); | |
var handler = eventField.FirstOrDefault(x => (x.FieldType.Name == eventHandler)); | |
if (handler != null) | |
{ | |
eventDelegate = (ElapsedEventHandler)handler.GetValue(classInstance); | |
} | |
return eventDelegate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment