zoukankan      html  css  js  c++  java
  • C# 反射

    反射 提供了对程序集,模块和类型的对象,可以使用反射动态创建类型的实例,或从现有的对象提供获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对他们进行访问。

    反射的性能损失主要来源于比较类型,遍历成员,调用成员三种情形,其中比较类型耗时最小,调用成员耗时最多,所以尽量减少采用成员动态调用等反射方式可以提高应用程序性能。除此之外,采取后期绑定,避免将反射方法放到循环内产生放大效应等办法均可以提升反射性能

    using Loger;
    using System;
    using System.Data;
    using System.Reflection;
    
    
    
    namespace Module1
    {
    
        public class Person
        {
            private int id = 0;
    
            public string Name
            {
                get;
                set;
            }
    
            public Person()
            {
    
            }
    
            public Person(string name)
            {
                this.Name = name;
            }
    
            public string GetName()
            {
                return this.Name;
            }
    
            public string GetValue(string prefix)
            {
                return prefix;
            }
    
            public bool MySex()
            {
                return true;
            }
    
        }
    
    }
    
    
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
            private static string instanceSpec = "System.EventArgs;System.Random;" + "System.Exception;System.Object;System.Version";
    
            static void Main(string[] args)
            {
    
                //Type t = Type.GetType("Module1.Person");
                //object[] para = new object[] { "Joey" };
                //Module1.Person p = (Module1.Person)Activator.CreateInstance(t, para);
                //Console.WriteLine(p.Name);
    
                Type t = Type.GetType("Module1.Person");
                object[] para = new object[] { "Joey" };
                object obj = Activator.CreateInstance(t, para);
                MethodInfo method = t.GetMethod("MySex");
                BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
                //object[] paraValue = new object[] { "Test" };
                object getValue = method.Invoke(obj, flag, Type.DefaultBinder, null, null);
    
    
    
                //Assembly asm = Assembly.LoadFrom(@"D:VSProjectsSourceVAULogerinDebugLoger.dll");
                //ILogInterface logger = null;
                //foreach (Type t in asm.GetTypes())
                //{
                //    //if (t.GetInterface("ILogInterface") != null)
                //    if (t.Name.Equals("JoelsLogger"))
                //    {
                //        logger = (ILogInterface)Activator.CreateInstance(t);
                //        break;
                //    }
                //}
                //if (logger != null)
                //    logger.WriteApplicationEvent("Initialized...");
    
                //Type t = Type.GetType("System.Data.DataTable,System.Data,Version=1.0.3300.0,  Culture=neutral,  PublicKeyToken=b77a5c561934e089");
                //DataTable table = (DataTable)Activator.CreateInstance(t);
    
    
                //string[] instances = instanceSpec.Split(';');
                //Array instlist = Array.CreateInstance(typeof(object), instances.Length);
                //object item;
                //for (int i = 0; i < instances.Length; i++)
                //{
                //    // create the object from the specification string
                //    Console.WriteLine("Creating instance of: {0}", instances[i]);
                //    item = Activator.CreateInstance(Type.GetType(instances[i]));
                //    instlist.SetValue(item, i);
                //}
                //Console.WriteLine("
    Objects and their default values:
    ");
                //foreach (object o in instlist)
                //{
                //    Console.WriteLine("Type:     {0}
    Value:    {1}
    HashCode: {2}
    ",
                //        o.GetType().FullName, o.ToString(), o.GetHashCode());
                //}
    
                Console.Read();
    
            }
        }
    }

    例子: 

    http://msdn.microsoft.com/zh-cn/magazine/cc163759(en-us).aspx

    http://www.cnblogs.com/wangshenhe/p/3256657.html

  • 相关阅读:
    js 图片压缩
    C#.NET中对称和非对称加密、解密方法汇总--亲测可用
    最精美详尽的 HTTPS 原理图
    swagger ui
    python爬取百度谷歌搜索结果
    Gitlab 11.0.3 迁移简明笔记
    ELK stack 7.6.x + kafka构建日志平台
    Nginx 内存占用高排查
    运维机器人 hubot 集成 jenkins
    运维机器人hubot,解放运维的神器
  • 原文地址:https://www.cnblogs.com/2zhyi/p/3544335.html
Copyright © 2011-2022 走看看