• Asp.Net 利用反射获得委托和事件以及创建委托实例和添加事件处理程序


    子程序定义:

          public delegate void CurrentControlListenEvent(string uniqueID, string way = null);
            public event CurrentControlListenEvent CurrentControlEvent;
            protected void InvokeCurrentControlEvent(string uniqueID, string way = null)
            {
                if (this.CurrentControlEvent != null)
                    CurrentControlEvent(this.UniqueID);
            }

    子程序调用:

    InvokeCurrentControlEvent("123456");
    
    //
    InvokeCurrentControlEvent("123456","del");

    父程序定义:

                 Control ctl = LoadControl("~/UserControls/Metadata/ucAttributeValues.ascx");
                        ctl.ID = "ucAttributeValues" + (tempLoadPhValueControlCount);
                        Type type = ctl.GetType();
    
                        EventInfo currentControlEvent = type.GetEvent("CurrentControlEvent");
                        Type tDelegate = currentControlEvent.EventHandlerType;
                        MethodInfo miHandler = this.GetType().GetMethod("InvokeControl");
                        Delegate d = Delegate.CreateDelegate(tDelegate, this, miHandler);
                        MethodInfo addHandler = currentControlEvent.GetAddMethod();
                        Object[] addHandlerArgs = { d };
                        addHandler.Invoke(ctl, addHandlerArgs);
    
                        //正常非反射的调用方式就一行代码,但有些情况不得不用反射来操作委托的实例
                        //ctl.SetCaptureListener(new UserControls_Metadata_ucAttributeValues.CaptureListenEvent(InvokeControl));

    委托子程序调用父程序的方法体:

        public void InvokeSubtract(string uniqueID)
            {
                if (this.phCtl.Controls.Count > 0)
                {
                    for (int i = this.phCtl.Controls.Count - 1; i >= 0; i--)
                    {
                        if (phCtl.Controls[i].UniqueID == uniqueID)
                        {
                            phCtl.Controls[i].Visible = false;
                            break;
                        }
                    }
                }
            }
  • 相关阅读:
    mplayer编程模式控制命令
    设置开发板启动后自启动Qt
    Linux下制作logo并显示到开发板上 .
    启动开发板,提示:can't access tty,job control turned off
    BellmanFord贝尔曼福特算法
    阿拉伯数字转中文数字
    webService
    http请求全过程
    (转)MongoDB设置访问权限、设置用户
    YTC, YTM, YTW
  • 原文地址:https://www.cnblogs.com/yonsy/p/5564280.html
Copyright © 2020-2023  润新知