Lats 说你有eventName,handlerMethodName,objectOnWhichTheEventIsDefined,objectOnWhichTheEventHandlerIsDefined。eventName和handlerMethodName是字符串,其余的对象是对象数据类型。然后,您可以使用反射将事件与处理程序连接起来,如下所示。
public bool SubscribeEvent(string eventName, string handlerMethodName,
object objectOnWhichTheEventIsDefined,
object objectOnWhichTheEventHandlerIsDefined)
{
try
{
var eventInfo = objectOnWhichTheEventIsDefined.GetType().GetEvent(eventName);
var methodInfo = objectOnWhichTheEventHandlerIsDefined.GetType().
GetMethod(handlerMethodName);
// Create new delegate mapping event to handler
Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, objectOnWhichTheEventHandlerIsDefined, methodInfo);
eventInfo.AddEventHandler(objectOnWhichTheEventIsDefined, handler);
return true;
}
catch (Exception ex)
{
// Log failure!
var message = "Exception while subscribing to handler. Event:" + eventName + " - Handler: " + handlerMethodName + "- Exception: " + ex;
Debug.Print(message);
return false;
}
}
一个完整的控制台示例如下所示。
class Program
{
static void Main(string[] args)
{
var typeWithEvent = new TypeWithEvent();
var typeWithEventHandler = new TypeWithEventHandler();
SubscribeEvent("EventTest", "EventHandlerMethod", typeWithEvent, typeWithEventHandler);
EventArgs e = new EventArgs();
Console.WriteLine("Event is about to be raised.");
typeWithEvent.OnTimeToRaiseTheEvent(e);
Console.WriteLine("Event trigger is completed.");
Console.ReadLine();
}
static bool SubscribeEvent(string eventName, string handlerMethodName, object objectOnWhichTheEventIsDefined, object objectOnWhichTheEventHandlerIsDefined)
{
try
{
var eventInfo = objectOnWhichTheEventIsDefined.GetType().GetEvent(eventName);
var methodInfo = objectOnWhichTheEventHandlerIsDefined.GetType().
GetMethod(handlerMethodName);
// Create new delegate mapping event to handler
Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, objectOnWhichTheEventHandlerIsDefined, methodInfo);
eventInfo.AddEventHandler(objectOnWhichTheEventIsDefined, handler);
return true;
}
catch (Exception ex)
{
// Log failure!
var message = "Exception while subscribing to handler. Event:" + eventName + " - Handler: " + handlerMethodName + "- Exception: " + ex;
Debug.Print(message);
return false;
}
}
}
internal class TypeWithEvent
{
public event EventHandler<EventArgs> EventTest;
internal void OnTimeToRaiseTheEvent(EventArgs e)
{
// Thread safe way to raise event as described in Events chapter 11 of
// the book CLR Via C#, 4th Edition, by Jeffrey Richter
EventHandler<EventArgs> temp = Volatile.Read(ref EventTest);
if (temp != null) temp(this, e);
}
}
internal class TypeWithEventHandler
{
public void EventHandlerMethod(Object sender, EventArgs e)
{
Console.WriteLine("From the event handler method.");
}
}