程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 解讀C#中的事件轉嫁

解讀C#中的事件轉嫁

編輯:.NET實例教程
/// <summary>
 /// 事件轉嫁類
 /// </summary>
 public class EventDeliver
 {
             /// <summary>
             /// 轉嫁事件
             /// </summary>
             /// <param name="eventDeclareObject">原始事件包含對象</param>
             /// <param name="eventDeclareName">原始事件名稱</param>
             /// <param name="eventHandleObject">目標事件響應對象</param>
             /// <param name="eventHandleName">目標事件響應方法</param>
             public static void Deliver(object eventDeclareObject,string eventDeclareName,object eventHandleObject, string eventHandleName)
             {
                          EventInfo eventInfo = eventDeclareObject.GetType().GetEvent(eventDeclareName,BindingFlags.Instance |BindingFlags.Static | BindingFlags.Public);
                          if (eventInfo == null)
                          {
                                       throw new ArgumentException();
          $False$

;            }
                          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("原始事件響應方法被調用");
             }
 }

//輸出效果

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved