zoukankan      html  css  js  c++  java
  • c# 元数据,反射

    元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等。

    反射 详细请查看这里

    测试程序

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stopwatch _timer;
    
                _timer = new Stopwatch();
                _timer.Start();
                
                Assembly asm = Assembly.GetExecutingAssembly();
                var t = asm.GetType("ConsoleApplication1.DynamicTest");
                object o = System.Activator.CreateInstance(t);//创建实例
                System.Reflection.MethodInfo mi = t.GetMethod("Add");//获得方法
                var r = mi.Invoke(o, new object[] {1, 2});
               
                for (int i = 0; i < 10000000; i++)
                {                
                    var re = mi.Invoke(o, new object[] { 1, 2 });
                }
                _timer.Stop();
                Console.WriteLine("Invoke方法" + _timer.ElapsedMilliseconds);
    
                _timer = new Stopwatch();
                _timer.Start();
                dynamic dynamicSample2 = new DynamicTest();
                for (int i = 0; i < 10000000; i++)
                {
                    var re2 = dynamicSample2.Add(1, 2);
                }
                _timer.Stop();
                Console.WriteLine("dynamic方法" + _timer.ElapsedMilliseconds);
                Console.ReadLine();            
            }
        }
    
        public class DynamicTest
        {
            public int Add(int a, int b)
            {
                return (a + b)/(a*b);
            }
        }
    }
    View Code

    测试结果:

    Invoke方法4374毫秒
    dynamic方法651毫秒

  • 相关阅读:
    LBS 经纬度定位
    LBS 经纬度定位
    GPS定位基本原理
    GPS定位基本原理
    Android Studio 之 启动和停止服务
    Android Studio 之 启动和停止服务
    【算法】最短路——两点最短总权和
    【算法】最短路——两点最短总权和
    【郑轻】[1743]解方程
    【郑轻】[1743]解方程
  • 原文地址:https://www.cnblogs.com/buyonesendone/p/3551404.html
Copyright © 2011-2022 走看看