zoukankan      html  css  js  c++  java
  • 反射初探

    一,类内部调用:

    注意点,此时 testFun为public属性

    private void button1_Click(object sender, EventArgs e)
    {
      MethodInfo dynMethod = this.GetType().GetMethod("testFun");
      dynMethod.Invoke(this, new object[] { 10, 20 });
    }

    public void testFun(int a, int b)
    {
      string msg = a.ToString() + "," + b.ToString();
      MessageBox.Show(msg);
    }

    若此时的 testFun为private属性,则 MethodInfo创建时要添加参数

    private void button1_Click(object sender, EventArgs e)
    {
      MethodInfo dynMethod = this.GetType().GetMethod("testFun", BindingFlags.NonPublic | BindingFlags.Instance);
      dynMethod.Invoke(this, new object[] { 10, 20 });
    }

    private void testFun(int a, int b)
    {
      string msg = a.ToString() + "," + b.ToString();
      MessageBox.Show(msg);
    }

     二 类外部调用

    注意点,此时 testFun为public属性

    private void button1_Click(object sender, EventArgs e)
    {
      HCTestClass theClass = new HCTestClass();
      Type theType = theClass.GetType();
      MethodInfo dynMethod = theType.GetMethod("testFun");
      dynMethod.Invoke(theClass, new object[] { 10, 20 });
    }

    class HCTestClass
    {
      public void testFun(int a, int b)
      {
        string msg = a.ToString() + "," + b.ToString();
        MessageBox.Show(msg);
      }
    }

     

    若此时的 testFun为private属性,则 MethodInfo创建时要添加参数

    private void button1_Click(object sender, EventArgs e)
    {
      HCTestClass theClass = new HCTestClass();
      Type theType = theClass.GetType();
      MethodInfo dynMethod = theType.GetMethod("testFun", BindingFlags.NonPublic | BindingFlags.Instance);
      dynMethod.Invoke(theClass, new object[] { 10, 20 });
    }

    class HCTestClass
    {
      private void testFun(int a, int b)
      {
        string msg = a.ToString() + "," + b.ToString();
        MessageBox.Show(msg);
      }
    }

     

     

    反射与函数直接调用性能比较

    函数直接调用性能,跑10000次在我的电脑上耗时约为10毫秒

    private void button1_Click(object sender, EventArgs e)
    {
      Stopwatch theWatch = new Stopwatch();
      theWatch.Start();
      for (int i = 0; i < 10000; i++)
      {
        testFun(10, 20);
      }

      theWatch.Stop();
      this.textBox1.Text = theWatch.ElapsedMilliseconds.ToString();
    }

    private void testFun(int a, int b)
    {
      string msg = a.ToString() + "," + b.ToString();
    }

    反射调用函数性能,跑10000次在我的电脑上耗时约为90毫秒

    private void button1_Click(object sender, EventArgs e)
    {
      Stopwatch theWatch = new Stopwatch();
      theWatch.Start();
      for (int i = 0; i < 10000; i++)
      {
        MethodInfo dynMethod = this.GetType().GetMethod("testFun", BindingFlags.NonPublic | BindingFlags.Instance);
        dynMethod.Invoke(this, new object[] { 10, 20 });
      }

      theWatch.Stop();
      this.textBox1.Text = theWatch.ElapsedMilliseconds.ToString();

    }

    private void testFun(int a, int b)
    {
      string msg = a.ToString() + "," + b.ToString();
    }

  • 相关阅读:
    iptables在不重新编译内核以及iptables的情况下 加载iptables模块--在proxmox5中没有测试成功
    ip rule以及ip route的使用--非常重要,需要持续研究!!
    debian版本的相关说明
    分析 linux服务器单网卡在可配置多网关 提供服务的可能性--重要!!!!!
    关于pn定期执行的相关问题分析
    man在线手册-推荐使用debian官方网站
    牛客网暑期ACM多校训练营(第三场) J Distance to Work 计算几何求圆与多边形相交面积模板
    杭电多校第二场 hdu 6315 Naive Operations 线段树变形
    牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径
    牛客网暑期ACM多校训练营(第三场) E Sort String 哈希处理字符串(模板)
  • 原文地址:https://www.cnblogs.com/LongHuaiYu/p/4878298.html
Copyright © 2011-2022 走看看