; }
if (eventInfo.GetAddMethod().IsStatic || eventInfo.GetRemoveMethod().IsStatic)
{
throw new ArgumentException();
}
Delegate handlerDelegate = Delegate.CreateDelegate(eventInfo.EventHandlerType,eventHandleObject,eventHandleName);
eventInfo.AddEventHandler(eventDeclareObject,handlerDelegate);
}
}
/// <summary>
/// 事件響應
/// </summary>
public class EventHandleObject
{
public void InvokerEventHandler(object sender, EventArgs e)
{
Console.WriteLine("轉嫁事件響應方法被調用");
}
}
/// <summary>
/// 事件源
/// </summary>
public class EventSourceObject
{
public event EventHandler SharpnessDotNetCallMe;
public void FireEvent()
{
SharpnessDotNetCallMe(null,null);
}
}
class Application
{
[STAThread]
static void Main(string[] args)
{
//原始方法
EventSourceObject sender1 = new EventSourceObject();
sender1.SharpnessDotNetCallMe += new EventHandler(sender1_SharpnessDotNetCallMe);
sender1.FireEvent();//觸發事件
//轉嫁方法
EventSourceObject sender2 = new EventSourceObject();
EventHandleObject handler = new EventHandleObject();
EventDeliver.Deliver(sender2,"SharpnessDotNetCallMe",handler,"InvokerEventHandler");
//觸發事件
sender2.FireEvent();
Console.Read();
}
private static void sender1_SharpnessDotNetCallMe(object sender, EventArgs e)
{
Console.WriteLine("原始事件響應方法被調用");
}
}
//輸出效果