• 学习笔记反射入门


    虽然反射一般情况用不到,但是咱能想了解下反射的工作原理

    首先定义一个类库ExampleLib,我就加了一个类Class1

    namespace ExampleLib
    {
        public class Class1
        {
            private string name;
            private int age;

            public Class1(string Name, int Age)
            {
                name = Name;
                age = Age;
            }

            public void changeName(string newName)
            {
                name = newName;
            }

            public void changeAge(int newAge)
            {
                age = newAge;
            }

            public override string ToString()
            {
                return string.Format("name:{0},age:{1}", name, age);
            }
        }
    }

    然后添加一个应用程序RfExamoleLib来分析这个类库

    namespace RfExamoleLib
    {
        class Program
        {
            //测试输出顺序<本函数跟这个反射的例子没有关系,呵呵...>
            public static string testFinally()
            {
                try
                {
                    string rtn = string.Empty;
                    //int x = int.Parse(rtn);
                    Console.WriteLine("try");
                    return "try";
                }
                catch (Exception)
                {
                    Console.WriteLine("catch");
                    //return "catch";
                }
                finally
                {
                    Console.WriteLine("finally");
                }
                return "zhe...";
            }

            static void Main(string[] args)
            {
                //加载程序集
                Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "ExampleLib.dll");
                //遍历
                Type[] tmpTypes = tmpAss.GetTypes();
                foreach (Type tmpType in tmpTypes)
                {
                    //获取第一个类型的构造函数信息
                    ConstructorInfo[] tmpConstructInfos = tmpType.GetConstructors();
                    foreach (ConstructorInfo tmpConstructInfo in tmpConstructInfos)
                    {
                        //为构造函数生成调用的参数集合
                        ParameterInfo[] tmpParameterInfos = tmpConstructInfo.GetParameters();
                        object[] tmpParameters = new object[tmpParameterInfos.Length];
                        for (int i = 0; i < tmpParameterInfos.Length; i++)
                        {

             //创建参数集合
                            tmpParameters[i] = tmpAss.CreateInstance(tmpParameterInfos[i].ParameterType.FullName);
                            if (tmpParameterInfos[i].ParameterType.FullName == "System.String")
                            {
                                tmpParameters[i] = "chen";
                            }
                            if (tmpParameterInfos[i].ParameterType.FullName == "System.Int32")
                            {
                                tmpParameters[i] = 29;
                            }
                        }

                        //实例化对象
                        object tmpObject = tmpConstructInfo.Invoke(tmpParameters);//构造函数实例化
                        Console.WriteLine(tmpObject);//执行重新的ToString()方法

                        //获取所有方法并执行
                        foreach (MethodInfo tmpMehdoInfo in tmpType.GetMethods())
                        {
                            //为方法的调用创建参数集合
                            tmpParameterInfos = tmpMehdoInfo.GetParameters();
                            tmpParameters = new object[tmpParameterInfos.Length];
                            for (int i = 0; i < tmpParameterInfos.Length; i++)
                            {
                                tmpParameters[i] = tmpAss.CreateInstance(tmpParameterInfos[i].ParameterType.FullName);
                                if (tmpParameterInfos[i].ParameterType.FullName == "System.String")
                                {
                                    tmpParameters[i] = "chenzhm";
                                }
                                if (tmpParameterInfos[i].ParameterType.FullName == "System.Int32")
                                {
                                    tmpParameters[i] = 30;
                                }
                            }
                            tmpMehdoInfo.Invoke(tmpObject, tmpParameters);
                        }
                        //调用方法后再次打印
                        Console.WriteLine(tmpObject);
                    }
                }
                //Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
                Console.WriteLine("call result:"+testFinally());

                Console.ReadLine();
            }
        }
    }

  • 相关阅读:
    React 高阶组件
    Facebook Graph API 接口请求
    (转载)HTML:模拟链接被按下,在新标签页打开页面,不使用window.open(可能被拦截)
    php file_put_contents() 写入回车
    mysql 慢查询开启
    FB接口之 js调用支付窗口
    8 个必备的PHP功能开发
    linux 负载 待读
    大话团队管理。
    API编排
  • 原文地址:https://www.cnblogs.com/maomaokuaile/p/2836991.html
Copyright © 2020-2023  润新知