C# 用反射动态绑定事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Diagnostics;

namespace WebApplication5
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EventHandler clickHandler = delegate(object s, EventArgs x) {
                var ctrl = s as WebControl;
                Debug.Print(ctrl.ID);
            };

            var click = Button1.GetType().GetEvents().FirstOrDefault(ei => ei.Name.ToLower() == "click");
            var handler = Delegate.CreateDelegate(clickHandler.GetType(), clickHandler.Method);
            click.AddEventHandler(Button1, handler);
        }
    }
}

 使用预定义委托:

public void registerUserActionLog(Control ctrl)
        {
            var click = ctrl.GetType().GetEvents().FirstOrDefault(ei => ei.Name.ToLower() == "click");
            if (click != null)
            {
                var method = this.GetType().GetMethod("ctrl_Click");
                var handler = Delegate.CreateDelegate(click.EventHandlerType, this, method);
                click.AddEventHandler(ctrl, handler);
            }

            foreach (Control c in ctrl.Controls)
            {
                registerUserActionLog(c);
            }
        }

        public void ctrl_Click(object sender, EventArgs e)
        {
            var ctrl = sender as WebControl;
            OMSLogManager.UserActionLogger.log(this.GetType().Name + "\t" + ctrl.ID + ".Click");
        }

 动态调整事件处理程序的调用顺序:

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var method = this.GetType().GetMethod("ctrl_Click");
            var click = Button1.GetType().GetEvents().FirstOrDefault(ei => ei.Name.ToLower() == "click");
            var handler = Delegate.CreateDelegate(click.EventHandlerType, null, method);
            var handlerList = new List();


            PropertyInfo propertyInfo = Button1.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
            EventHandlerList eventHandlerList = propertyInfo.GetValue(Button1, new object[] { }) as EventHandlerList;
            FieldInfo fieldInfo = typeof(Button).GetField("EventClick", BindingFlags.NonPublic | BindingFlags.Static);
            if(fieldInfo == null)
                fieldInfo = typeof(Button).GetField("Click", BindingFlags.NonPublic | BindingFlags.Static);
            if (fieldInfo != null)
            {
                object eventKey = fieldInfo.GetValue(Button1);
                var del = eventHandlerList[eventKey] as Delegate;
                foreach (var h in del.GetInvocationList())
                {
                    //preserve previous eventhandler
                    handlerList.Add(h);

                    //remove previous eventhandler
                    click.RemoveEventHandler(Button1, h);
                }
            }


            //add new eventhandler
            click.AddEventHandler(Button1, handler);

            //add previous eventhandler
            foreach (var h in handlerList)
            {
                click.AddEventHandler(Button1, h);
            }
        }

        public void ctrl_Click(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }
    }

 

转载于:https://www.cnblogs.com/nanfei/p/6400429.html

你可能感兴趣的:(C# 用反射动态绑定事件)