C#_反射补充

测试泛型类:

 public class GenericTest<T, W, X>
    {

        public void Show(T t, W w, X x)
        {
            Console.WriteLine("t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name);
        }

    }
    //泛型方法
    public class GenericMethod
    {
        public void Show<T, W, X>(T t, W w, X x)
        {
            Console.WriteLine("t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name);
        }
    }

    public class GenericDouble<T>
    {
        public void Show<W, X>(T t, W w, X x)
        {
            Console.WriteLine("t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name);
        }
    }

测试普通类普通方法:

public class ReflectionTest
    {

        #region Identity
        /// 
        /// 无参构造函数
        /// 
        public ReflectionTest()
        {
            Console.WriteLine("这里是{0}无参数构造函数", this.GetType());
        }

        /// 
        /// 带参数构造函数
        /// 
        /// 
        public ReflectionTest(string name)
        {
            Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
        }

        public ReflectionTest(int id)
        {
            Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
        }


        public ReflectionTest(int id, string name)
        {

            //typeof(int);

            //Type //id.GetType();

            Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
        }

        #endregion

        #region Method
        /// 
        /// 无参方法
        /// 
        public void Show1()
        {
            Console.WriteLine("这里是{0}的Show1", this.GetType());
        }
        /// 
        /// 有参数方法
        /// 
        /// 
        public void Show2(int id)
        {

            Console.WriteLine("这里是{0}的Show2", this.GetType());
        }
        /// 
        /// 重载方法之一
        /// 
        /// 
        /// 
        public void Show3(int id, string name)
        {
            Console.WriteLine("这里是{0}的Show3", this.GetType());
        }
        /// 
        /// 重载方法之二
        /// 
        /// 
        /// 
        public void Show3(string name, int id)
        {
            Console.WriteLine("这里是{0}的Show3_2", this.GetType());
        }
        /// 
        /// 重载方法之三
        /// 
        /// 
        public void Show3(int id)
        {

            Console.WriteLine("这里是{0}的Show3_3", this.GetType());
        }
        /// 
        /// 重载方法之四
        /// 
        /// 
        public void Show3(string name)
        {

            Console.WriteLine("这里是{0}的Show3_4", this.GetType());
        }
        /// 
        /// 重载方法之五
        /// 
        public void Show3()
        {

            Console.WriteLine("这里是{0}的Show3_1", this.GetType());
        }
        /// 
        /// 私有方法
        /// 
        /// 
        private void Show4(string name)  //肯定是可以的
        {
            Console.WriteLine("这里是{0}的Show4", this.GetType());
        }
        /// 
        /// 静态方法
        /// 
        /// 
        public static void Show5(string name)
        {
            Console.WriteLine("这里是{0}的Show5", typeof(ReflectionTest));
        }
        #endregion
    }
            try
            {
                #region 反射调用无参无返回值方法
                //动态加载DLL
                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");
                //获取指定类型
                Type type = assembly.GetType("TestCommon.ReflectionTest");
                //获取指定方法
                MethodInfo method = type.GetMethod("Show1");
                //获取有参构造函数
                ConstructorInfo info = type.GetConstructor(new Type[] { typeof(int) });
                //创建对象
                object oTest = info.Invoke(new object[] { 123 });
                //调用方法
                method.Invoke(oTest, null);

                #endregion

                #region 反射调用有参方法
                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");
                Type type = assembly.GetType("TestCommon.ReflectionTest");
                //使用默认构造函数
                object oTest = Activator.CreateInstance(type);
                //获取有参方法
                MethodInfo method = type.GetMethod("Show2");
                method.Invoke(oTest, new object[] { 2 });
                #endregion

                #region 反射调用重载方法普通方法
                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");
                Type type = assembly.GetType("TestCommon.ReflectionTest");
                //调用有参构造函数
                object oTest = Activator.CreateInstance(type, new object[] { 888, "zs" });//直接赋值参数,自动去找有对应参数的构造函数去
                //MethodInfo method=type.GetMethod("Show3");//重载方法需指明调用目标
                MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(int), typeof(string) });
                MethodInfo method1 = type.GetMethod("Show3", new Type[] { typeof(int) });
                MethodInfo method2 = type.GetMethod("Show3", new Type[] { typeof(string) });
                //调用
                method.Invoke(oTest, new object[] { 14, "zs" });
                #endregion

                #region 反射调用私有方法
                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");
                Type type = assembly.GetType("TestCommon.ReflectionTest");
                object oTest = Activator.CreateInstance(type);
                //获取私有方法
                //MethodInfo method=type.GetMethod("Show4");//对于私有方法直接获取是不行的
                //BindingFlags.Public|BindingFlags.Instance 默认查找public、instance内容
                //如果要查找私有成员  需要指定BindingFlags查找非公共的实例成员BindingFlags.NonPublic|BindingFlags.Instance
                //如果只设置 BindingFlags.NonPublic,覆盖默认设置 就不会查找实例成员了 也不会成功查到
                MethodInfo method = type.GetMethod("Show4", BindingFlags.NonPublic | BindingFlags.Instance);

                method.Invoke(oTest, new object[] { "Peter" });
                #endregion

                #region 反射调用静态成员
                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");
                Type type = assembly.GetType("TestCommon.ReflectionTest");
                //搜索静态成员和公共成员
                //MethodInfo method=type.GetMethod("Show5", BindingFlags.Static | BindingFlags.Public);//OK
                MethodInfo method = type.GetMethod("Show5");
                object oTest = Activator.CreateInstance(type);
                //method.Invoke(oTest, new object[]{ "zs" });  ok

                //因为是静态成员,所以调用的时候可以不需要对象
                method.Invoke(null, new object[] { "zss" });
                #endregion

                #region 反射调用泛型方法
                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");
                Type type = assembly.GetType("TestCommon.GenericMethod");
                //获取泛型方法
                MethodInfo method = type.GetMethod("Show");
                //为泛型方法指定数据类型,替换T,W,S
                MethodInfo GenericMethod = method.MakeGenericMethod(new Type[] { typeof(int), typeof(string), typeof(DateTime) });
                object oTest = Activator.CreateInstance(type);
                //method.Invoke(oTest, new object[] { 12,"zs",DateTime.Now });//因为不确定参数类型(或未指定),调用出错

                //调用泛型方法
                GenericMethod.Invoke(oTest, new object[] { 12, "121212", DateTime.Now });

                #endregion

                #region 反射调用泛型类中的普通方法
                //看一下泛型类的全名称
                Console.WriteLine(typeof(GenericTest<string, string, int>));
                //TestCommon.GenericTest`3[System.String,System.String,System.Int32]

                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");

                //Type type=assembly.GetType("GenericTest.GenericTest");//获取不了
                Type type = assembly.GetType("TestCommon.GenericTest`3");
                //Console.WriteLine(type);//TestCommon.GenericTest`3[T,W,X]

                //为泛型类型参数赋值得到泛型类型
                Type GenericType = type.MakeGenericType(new Type[] { typeof(string), typeof(string), typeof(int) });
                MethodInfo method = GenericType.GetMethod("Show");

                object oTest = Activator.CreateInstance(GenericType);

                //调用-->参数引用类的泛型类型,所以对应上就行 public void Show(T t, W w, X x) { } 
                method.Invoke(oTest, new object[] { "peter", "jakey", 12 });
                #endregion

                #region 调用泛型类中的泛型方法 
                //public class GenericDouble
                //{
                //    public void Show(T t, W w, X x)
                //    {
                //        Console.WriteLine("t.type={0},w.type={1},x.type={2}", t.GetType().Name, w.GetType().Name, x.GetType().Name);
                //    }
                //} 

                //看一下该泛型类型的TYPE全名称
                //TestCommon.GenericDouble`1[System.String]
                //---------------------------------------------------`标识泛型(类),1-->泛型类的参数个数-----。
                Console.WriteLine(typeof(GenericDouble<string>));

                Assembly assembly = Assembly.LoadFrom("TestCommon.dll");
                //获取类型
                Type type = assembly.GetType("TestCommon.GenericDouble`1");
                //指定泛型类型
                Type GenericType = type.MakeGenericType(new Type[] { typeof(string) });
                object oTest = Activator.CreateInstance(GenericType);

                //获取泛型方法
                MethodInfo method = GenericType.GetMethod("Show");
                //为泛型方法指定泛型参数类型并获得该泛型方法
                MethodInfo GenericMethod = method.MakeGenericMethod(new Type[] { typeof(string), typeof(string) });

                //调用泛型方法
                GenericMethod.Invoke(oTest, new object[] {"peter","jakey","sunday"});           
                #endregion 

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                Console.ReadKey();
            }

你可能感兴趣的:(C#,笔记,c#,反射,.net,类)