学习笔记反射入门

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

首先定义一个类库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();
        }
    }
}

原文地址:https://www.cnblogs.com/maomaokuaile/p/2836991.html