• 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 + "	" + 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<Delegate>();
    
    
                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)
            {
                
            }
        }
  • 相关阅读:
    Python字典
    Python集合
    Hungray匈牙利算法
    异常捕获模块,throw, try, catch, finally
    《加德纳艺术通史》罗杰克-劳利
    《高效休息法》久贺谷亮
    梯度检验 Gradient check,bias correction, Exponentially Weighted Averages
    方差与偏差,bias vs variance
    大数定理,中心极限定理以及一些常见分布
    聚类-均值漂移
  • 原文地址:https://www.cnblogs.com/nanfei/p/6400429.html
Copyright © 2020-2023  润新知