zoukankan      html  css  js  c++  java
  • 反射

    一、反射的概述

    通过反射可以提供类型信息,从而使得我们开发人员在运行时能够利用这些信息构造和使用对象。 

    反射机制允许程序在执行过程中动态地添加各种功能。 

    二、通过反射创建对象

    创建一个类

    using System;
    
    namespace StudentTest
    {
        public class Student
        {
            public string Name { get; set; }
            public Student()
            {
                Console.WriteLine("我是无参构造函数");
            }
            public Student(int v)
            {
                Console.WriteLine("我是有参构造函数");
            }
        }
    }

    通过反射创建Student对象,调用里面的方法

     static void Main(string[] args)
            {
                //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.Student");
                //创建对象
                object obj = Activator.CreateInstance(type);
                var student = obj as Student;
                student.Test();
                Console.ReadLine();
            }

    二、通过反射创建有参对象

     static void Main(string[] args)
            {
                //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.Student");
                
                //获取对象下的所有构造方法
                foreach(ConstructorInfo constructorInfo in type.GetConstructors())
                {
                    Console.WriteLine(constructorInfo.Name);
                    //获取构造方法的所有参数类型
                    foreach(var parameterInfo  in constructorInfo.GetParameters())
                    {
                        Console.WriteLine(parameterInfo.ParameterType);
    
                    }
                }
                //创建有参构造函数
                object obj1 = Activator.CreateInstance(type,new object[] { 111});
                Console.ReadLine();
            }

    三、通过反射创建私有对象

    //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.Teacher");
                //创建私有构造函数
                object obj1 = Activator.CreateInstance(type,true);
                Console.ReadLine();

    四、通过反射创建泛型类

     //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.GenericClass");
                Type makeType = type.MakeGenericType(new Type[] {typeof(int) });
    
                //创建泛型对象
                object obj1 = Activator.CreateInstance(makeType);

     五、通过反射调用方法

    static void Main(string[] args)
            {
                //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.Student");
                //得到所有方法
                foreach (var MethodInfo in type.GetMethods())
                {
                    Console.WriteLine(MethodInfo.Name);
                    //得到方法参数
                    foreach (var item in MethodInfo.GetParameters())
                    {
                        Console.WriteLine(MethodInfo.Name + " " + item.ParameterType);
                    }
                }
                object obj = Activator.CreateInstance(type);
                //MethodInfo methodInfo = type.GetMethod("Test1");
                //methodInfo.Invoke(obj,null);
                //有参数
                //MethodInfo methodInfo = type.GetMethod("Test2");
                //methodInfo.Invoke(obj,new object[]{111});
                //重载方法调用
                //MethodInfo methodInfo = type.GetMethod("Test3",new Type[] {typeof(int),typeof(string) });
                //methodInfo.Invoke(obj, new object[] { 111,"111"});
                //静态
                //MethodInfo methodInfo = type.GetMethod("Test4");
                //methodInfo.Invoke(obj, new object[] { 111 });
    
                //私有方法调用
                MethodInfo methodInfo = type.GetMethod("Test5", BindingFlags.Instance | BindingFlags.NonPublic);
                methodInfo.Invoke(obj, new object[] { 111 });
                Console.ReadLine();
            }
    public class Student
        {
            public string Name { get; set; }
            public Student()
            {
                Console.WriteLine("我是无参构造函数");
            }
            public Student(int v)
            {
                Console.WriteLine("我是有参构造函数");
            }
            public void Test1()
            {
                Console.WriteLine("我是一个方法");
            }
            public void Test2(int id)
            {
                Console.WriteLine("我是一个有参方法");
            }
            public void Test3(int id,string name)
            {
                Console.WriteLine("我是一个重载int string方法");
            }
            public void Test3(int id, int name)
            {
                Console.WriteLine("我是一个重载方法");
            }
            public static void Test4(int id)
            {
                Console.WriteLine("我是一个静态方法");
            }
        }

    六、通过反射调用泛型方法

    static void Main(string[] args)
            {
                //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.GenericClass2");
    
                object obj = Activator.CreateInstance(type);
                //泛型方法
                MethodInfo methodInfo = type.GetMethod("Test");
                var methodGeneric = methodInfo.MakeGenericMethod(new Type[] { typeof(int)});
                methodGeneric.Invoke(obj, new object[] { 11 });
                Console.ReadLine();
            }
        public class GenericClass2
        {
            public void Test<T>(T id)
            {
                Console.WriteLine("泛型方法");
            }
        }

    七、通过反射调用泛型类泛型方法

    static void Main(string[] args)
            {
                //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.GenericClass`1");
                Type type1 = type.MakeGenericType(new Type[] { typeof(int)});
                object obj = Activator.CreateInstance(type1);
                //泛型方法
                MethodInfo methodInfo = type1.GetMethod("Test");
                var methodGeneric = methodInfo.MakeGenericMethod(new Type[] { typeof(int)});
                methodGeneric.Invoke(obj, new object[] { 11 });
                Console.ReadLine();
            }
        public class GenericClass<T>
        {
            public void Test<T>(T id)
            {
                Console.WriteLine("泛型类泛型方法");
            }
        }

    八、通过反射操作字段

    static void Main(string[] args)
            {
                Student student = new Student
                {
                    Id = 11,
                    Name = "测试一哈"
                };
                //加载dll文件
                Assembly assembly = Assembly.LoadFrom("StudentTest.dll");
                //Student
                Type type = assembly.GetType("StudentTest.Student");
                object obj = Activator.CreateInstance(type);
                foreach(var prop in type.GetProperties())
                {
                    Console.WriteLine($"{prop.PropertyType}+{prop.Name}+{prop.GetValue(student)}");
                    if (prop.Name.Equals("Id"))
                    {
                        prop.SetValue(student,22);
                    }
                    if (prop.Name.Equals("Name"))
                    {
                        prop.SetValue(student, "哈哈哈");
                    }
                    Console.WriteLine($"{prop.PropertyType}+{prop.Name}+{prop.GetValue(student)}");
                }
                Console.ReadLine();
            }
     public class Student
        {
            public string Name { get; set; }
            public int Id { get; set; }
        }
  • 相关阅读:
    thinkphp在wamp 配置去掉url中index.php方法
    mysql新监语句需要前面加SET FOREIGN_KEY_CHECKS=0;
    ini_set的用法介绍
    SpringBoot项目启动报错:java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    我的分享:第八章: 用Redis轻松实现秒杀系统
    项目分享:第一章:个人电商项目
    SpringBoot框架:第二章:SpringBoot中static和templates二个目录下的页面和静态资源访问的三个常见问题
    我的分析:第八章:JAVA高级工程师知识点
    Springboot项目启动报org.springframework.beans.factory.UnsatisfiedDependencyException
    Disconnected from the target VM, address: '127.0.0.1:56577', transport: 'socket'
  • 原文地址:https://www.cnblogs.com/liguix/p/14753077.html
Copyright © 2011-2022 走看看