反射备忘

反射: 通过 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太复杂啦. 个人不去研究了哭泣的脸

posted @ 2011-07-06 17:46  MyCoolDog  阅读(320)  评论(0编辑  收藏  举报