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();
    }

  • 相关阅读:
    支付宝接口相关整理
    诡异的 &quot;password取回&quot; 邮件问题
    剑指 offer代码解析——面试题39推断平衡二叉树
    女程序猿做了个梦,各路大神惊现神级评论!
    Mac: Android studio+VirtualBox+Genymotion
    lua 中pairs 和 ipairs差别
    機器學習基石 (Machine Learning Foundations) 作业1 Q15-17的C++实现
    怎样获取HTML5视频的持续时间
    Android之本地相冊图片选取和拍照以及图片剪辑
    19_Android中图片处理原理篇,关于人脸识别站点,图片载入到内存,图片缩放,图片翻转倒置,网上撕衣服游戏案例编写
  • 原文地址:https://www.cnblogs.com/LongHuaiYu/p/4878298.html
Copyright © 2011-2022 走看看