zoukankan      html  css  js  c++  java
  • C# 调用一个按钮的Click事件(利用反射)

    最基本的调用方法

    (1)button1.PerformClick();
    (2)button1_Click(null,null);
    (3)button_Click(null,new EventArgs());

    利用反射调用

    最开始的调用方法

    //建立一个函数  
    private void callOnClick(Button btn)  
    {  
        //建立一个类型  
        Type t = typeof(Button);  
        //参数对象  
        object[] p = new object[1];  
        //产生方法  
        MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);  
        //参数赋值。传入函数  
        p[0] = EventArgs.Empty;  
        //调用  
        m.Invoke(btn, p);  
        return;  
    }  
      
    //调用例子。  
    //调用Button1的onclick  
    callOnClick(Button1);  
      
    //调用Button5的onclick  
    callOnClick(Button5);  

    扩展一下,可以调用更多的事件。

    private void callButtonEvent(Button btn, string EventName)  
    {     
        //建立一个类型      
        Type t = typeof(Button);  
        //参数对象      
        object[] p = new object[1];  
        //产生方法      
        MethodInfo m = t.GetMethod(EventName, BindingFlags.NonPublic | BindingFlags.Instance);  
        //参数赋值。传入函数      
        //获得参数资料  
        ParameterInfo[] para =  m.GetParameters();  
        //根据参数的名字,拿参数的空值。  
        p[0] = Type.GetType(para[0].ParameterType.BaseType.FullName).GetProperty("Empty");  
        //调用      
        m.Invoke(btn, p);  
        return;  
    }  
      
    //调用例子。  
    //调用Button1的onclick  
    callButtonEvent(Button1,"OnClick");  
      
    //调用Button5的OnKeyPress  
    callButtonEvent(Button5,"OnKeyPress");  

    现在,再来一次扩展。可以处理Button,TextBox等等的一些事件。

    private void callObjectEvent(Object obj, string EventName)  
    {     
         //建立一个类型,AssemblyQualifiedName拿出有效的名字     
         Type t = Type.GetType(obj.GetType().AssemblyQualifiedName);  
         //参数对象      
         object[] p = new object[1];  
         //产生方法      
         MethodInfo m = t.GetMethod(EventName, BindingFlags.NonPublic | BindingFlags.Instance);  
         //参数赋值。传入函数      
         //获得参数资料  
         ParameterInfo[] para =  m.GetParameters();  
         //根据参数的名字,拿参数的空值。  
         p[0] = Type.GetType(para[0].ParameterType.BaseType.FullName).GetProperty("Empty");  
         //调用      
         m.Invoke(obj, p);  
         return;  
    }  
      
    //调用例子。  
    //调用Button1的onclick  
    callObjectEvent(Button1,"OnClick");  
      
    //调用Button5的OnKeyPress  
    callObjectEvent(Button5,"OnKeyPress");  
      
    //调用Text1的OnTextChanged  
    callObjectEvent(Text1, "OnTextChanged");  

    继续来多一个扩展。可以传入事件参数。

    private void callObjectEvent(Object obj, string EventName, EventArgs e=null)  
    {     
        //建立一个类型      
        //Type t = typeof(obj.GetType);  
        Type t = Type.GetType(obj.GetType().AssemblyQualifiedName);  
        //产生方法      
        MethodInfo m = t.GetMethod(EventName, BindingFlags.NonPublic | BindingFlags.Instance);  
        //参数赋值。传入函数      
        //获得参数资料  
        ParameterInfo[] para =  m.GetParameters();  
        //根据参数的名字,拿参数的空值。  
        //参数对象      
        object[] p = new object[1];  
        if (e == null)  
            p[0] = Type.GetType(para[0].ParameterType.BaseType.FullName).GetProperty("Empty");  
        else  
            p[0] = e;  
        //调用  
        m.Invoke(obj, p);  
        return;  
    }  
      
    //调用例子。  
    //调用Button1的onclick  
    callObjectEvent(Button1,"OnClick");  
    //调用Button5的OnKeyPress  
    callObjectEvent(Button5,"OnKeyPress");  
    //调用Text1的OnTextChanged  
    callObjectEvent(Text1, "OnTextChanged");  
    //调用Form的KeyPress事件, this就是那个winform, 并且传入回车键  
    callObjectEvent(this, "OnKeyPress", new KeyPressEventArgs((char)13));  

    测试的时候,是在winform里的控件的调用。在程序里要加入

    using System.Reflection;  

    引用自 :http://blog.csdn.net/dogfish/article/details/7048280

  • 相关阅读:
    Linux下C语言执行shell命令
    netstat实现原理
    安装docker
    springboot启动提示连接mysql报错:java.sql.SQLNonTransientConnectionException: CLIENT_PLUGIN_AUTH is required
    linux删除用户报错:userdel: user prize is currently used by process 28021
    centos的6.9版本安装openjdk1.8
    centos的6.9版本安装mysql
    安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or
    安装docker报错:https://download.docker.com/linux/centos/7/i386/stable/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22
    linux配置docker报错:ImportError: No module named yum
  • 原文地址:https://www.cnblogs.com/feiyuhuo/p/5407544.html
Copyright © 2011-2022 走看看