zoukankan      html  css  js  c++  java
  • 反射备忘

    反射: 通过 System.Reflection 命名空间中的类以及 System..::.Type,您可以获取有关已加载的程序集和在其中定义的类型(如类、接口和值类型)的信息。您也可以使用反射在运行时创建类型实例,以及调用和访问这些实例。

    namespace _Net的反射
    {
        
     
        public class Person {
            public void Action() {
                Console.WriteLine("People Run!");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                int times = 500000;
                var p = new Person();
                //获得类型的几种方法
                
                Type t = typeof(Person);
                t = Type.GetType("_Net的反射.Person");
                t = p.GetType();
     
                //实例化对象的各种方法
                Stopwatch stop = new Stopwatch(); stop.Start();
                for (int i = 0; i < times; i++)
                {
                    var per = new Person();
                }
                stop.Stop();
                Console.WriteLine("直接实例化耗时"+System.Environment.NewLine+stop.ElapsedMilliseconds);
                stop.Reset();
     
                stop.Start();
                for (int i = 0; i < times; i++)
                {
                    var per = Activator.CreateInstance(typeof(Person));
                }
                stop.Stop();
                Console.WriteLine("Activator.CreateInstance(typeof(Person))实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
                stop.Reset();
     
                stop.Start();
                for (int i = 0; i < times; i++)
                {
                    var per = typeof(Person).GetConstructor(Type.EmptyTypes).Invoke(null);
                }
                stop.Stop();
                Console.WriteLine("typeof(Person).GetConstructor(Type.EmptyTypes).Invoke(null)实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
                stop.Reset();
     
                stop.Start();
                for (int i = 0; i < times; i++)
                {
                    var per =Type.GetType("_Net的反射.Person").GetConstructor(Type.EmptyTypes).Invoke(null);
                }
                stop.Stop();
                Console.WriteLine("Type.GetType(_Net的反射.Person).GetConstructor(Type.EmptyTypes).Invoke(null)实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
                stop.Reset();
     
                
                stop.Start();
                for (int i = 0; i < times; i++)
                {
                    var per = Activator.CreateInstance(Type.GetType("_Net的反射.Person"));
                }
                stop.Stop();
                Console.WriteLine(" Activator.CreateInstance(Type.GetType(_Net的反射.Person))实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
                stop.Reset();
     
                //stop.Start();
                //for (int i = 0; i < times; i++)
                //{
                //    var per = System.Reflection.Assembly.Load("_Net的反射").CreateInstance("_Net的反射.Person");
                //}
                //stop.Stop();
                //Console.WriteLine(" Assembly.Load(_Net的反射).CreateInstance(_Net的反射.Person)实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
                //stop.Reset();
     
                stop.Start();
                var la=GetCreateFunc();
                for (int i = 0; i < times; i++)
                {
                    var per = la();
                }
                stop.Stop();
                Console.WriteLine(" Func<Person> GetCreateFunc()实例化耗时" + System.Environment.NewLine + stop.ElapsedMilliseconds);
                stop.Reset();
     
                Console.ReadKey();
            }
     
            
            //表达式实例化对象
            static Func<Person> GetCreateFunc() {
                var newExpression = Expression.New(typeof(Person));
                return Expression.Lambda<Func<Person>>(newExpression, null).Compile();
            }
     
        }
    }

    image

    至于EMIT太复杂啦. 个人不去研究了哭泣的脸

  • 相关阅读:
    LINUX 常用命令
    连接远程Linux的几类工具
    spring-boot导出excel
    基于Vue2全家桶的移动端AppDEMO实现
    jdk+tomcat+mysql+war打包整合成exe文件,Windows下一键安装
    使用 Gogs 搭建自己的 Git 服务器
    db2 命令
    在shell脚本中调用另一个脚本的三种不同方法(fork, exec, source)
    Linux shell break、continue、exit、return的用法 及exit、return的区别
    redis 导入导出redis-load,redis-dump详解
  • 原文地址:https://www.cnblogs.com/jianjialin/p/2099429.html
Copyright © 2011-2022 走看看