反射-2

 private void button1_Click(object sender, EventArgs e)
        {
            Type tDog = typeof(Dog);
            Type tCat = typeof(Cat);
            //用Activator创建一个类型对象,这个类必须包含一个无参构造函数

            Dog d1 = Activator.CreateInstance(tDog) as Dog;

            Cat c1 = Activator.CreateInstance(tCat) as Cat;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Dog d1 = new Dog() { name = "小白", age = 3, Gender = false };
            ShowObjFields(d1);

        }
        public void ShowObjFields(object o)
        {
            Type tObj = o.GetType();
            System.Text.StringBuilder sb = new StringBuilder(20);
            //2.获取对象所属类的所有对象
            FieldInfo[] fields = tObj.GetFields();
            foreach (FieldInfo f in fields)
            {
                sb.Append(f.Name + "=" + f.GetValue(o) + "
");
            }
            MessageBox.Show(sb.ToString());
        }
        //获取所有属性
        private void button3_Click(object sender, EventArgs e)
        {
            Dog d1 = new Dog() { name = "小白", age = 3, Gender = false };
            ShowObjProperties(d1);
        }
        public void ShowObjProperties(object o)
        {
            Type tObj = o.GetType();
            System.Text.StringBuilder sb = new StringBuilder(20);
            //2.获取对象所属类的所有对象
            PropertyInfo[] PropertyInfos = tObj.GetProperties();
            foreach (PropertyInfo f in PropertyInfos)
            {
                sb.Append(f.Name + "=" + f.GetValue(o) + "
");
            }
            MessageBox.Show(sb.ToString());
        }
        /// <summary>
        /// 给字段赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            Type dType = typeof(Dog);
            Dog d = new Dog();
            FieldInfo fname = dType.GetField("name");
            fname.SetValue(d, "花花");
            MessageBox.Show(d.name);

            //为指定的属性赋值
            PropertyInfo pHoby = dType.GetProperty("Hoby");
            pHoby.SetValue(d, "咬骨头");
            MessageBox.Show(d.Hoby);
        }
        #region 加载方法
        /// <summary>
        /// 加载方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            Dog d1 = new Dog() { name = "小白", age = 3, Gender = false };
            FillCBBWithObjMethodName(d1);
        }
        /// <summary>
        /// 根据对象 获取方法 
        /// </summary>
        /// <param name="o"></param>
        public void FillCBBWithObjMethodName(object o)
        {
            Type tObj = o.GetType();
            //获取 类中 直接定义的,实例的,公共方法
            MethodInfo[] methods = tObj.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
            foreach (MethodInfo m in methods)
            {
                comboBox1.Items.Add(m.Name);
            }
        }
        #endregion

        #region 调用方法
        private void button6_Click(object sender, EventArgs e)
        {
            Dog d1 = new Dog() { name = "小白", age = 3, Gender = false };
            InvokeMethodByName(d1);

        }
        public void InvokeMethodByName(object o)
        {
            string StrMethod = comboBox1.Text;//获取选中的方法名
            Type tObj = o.GetType();
            ///获取指定的方法对象
            MethodInfo method = tObj.GetMethod(StrMethod);
            string strRes = method.Invoke(o, null).ToString();//o就是为类里的this赋值
            MessageBox.Show(strRes);
        }
        #endregion

        #region 获取构造函数
        private void button7_Click(object sender, EventArgs e)
        {
            Type tDog = typeof(Dog);
            FillConstructor(tDog);
        }
        void FillConstructor(Type t)
        {
            //1获取所有公有的构造函数
            ConstructorInfo[] constructors = t.GetConstructors();
            //2遍历 填充构造函数
            foreach (ConstructorInfo c in constructors)
            {
                comboBox2.Items.Add(c.GetParameters().Length.ToString());
            }
        }
        #endregion

        #region 调用构造函数
        private void button8_Click(object sender, EventArgs e)
        {
            Type tDog = typeof(Dog);
            int strParasLength = int.Parse(comboBox2.Text);
            ConstructorInfo constructorInfo = null;
            switch (strParasLength)
            {
                case 0:
                    constructorInfo = tDog.GetConstructor(null);
                    break;
                case 1:
                    constructorInfo = tDog.GetConstructor(new Type[1] { typeof(string) });
                    break;
                case 2:
                    constructorInfo = tDog.GetConstructor(new Type[2] { typeof(string), typeof(int) });
                    break;
            }
            Dog d = null;
            //通过调用指定参数构造函数 ,创建DOg对象
            if (constructorInfo != null)
            {
                switch (strParasLength)
                {
                    case 0:
                        d = constructorInfo.Invoke(null) as Dog; //构造函数返回的是类的对象
                        break;
                    case 1:
                        d = constructorInfo.Invoke(new object[1] { "小黑" }) as Dog;
                        break;
                    case 2:
                        d = constructorInfo.Invoke(new object[2] { "小白", 3 }) as Dog;
                        break;
                }
            }
            string res = d.ShoutHi();
            MessageBox.Show(res);
        }
        #endregion
原文地址:https://www.cnblogs.com/sumg/p/3800968.html