反射

1. 反射可以做什么:

        .net 类都被编译诚IL,反射就可以在运行时获得类的信息(有哪些方法,字段,构造函数,父类),可以动态创建对象,动态赋值,动态调用方法

         每一个类对应一个type对象,每一个方法对应一个MethodInfo 对象,每个属性对应一个PropertyInfo 这就是类、方法、属性的“元数据”。对象和这个类的对象没有直接的关系。这些元数据对象和成员有关系,和对象无关。

    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            Person p2 = new Person();
            Person p3 = new Person();

            Type t1 = p1.GetType();
            Type t2 = typeof(Person);
            Type t3 = Type.GetType("ConsoleApp12.Person");
            // Activator.CreateInstance(t1) 动态创建类,类必须有public且无参构造函数
            object obj = Activator.CreateInstance(t1);//动态创建t1指向的类的对象。new Person()
            Console.WriteLine(obj);
            Console.WriteLine(t1);
            Console.ReadKey();
        } 
         

    }
     

    class Person  //一个类对应一个type对象
    {
        public string Name { get; set; }
        public int Age { get; set; }

    }

  2. this  关键字:

      

    class Parent   
    {

        public void Hello()
        {
           // Type type = this.GetType(); //this 不是代表当前类;而是代表"当前对象",this 看做当前对象的类

            this.Test();
        }
        public virtual void Test()
        {
            Console.WriteLine("parent test");
        }

    }
    class Child1:Parent
    {
        public override void Test()
        {
            Console.WriteLine("child1 test");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Child1 child1 = new Child1();
            child1.Hello();
          //  Console.WriteLine();
            Console.ReadKey();
        } 
         

    }

3.   Type的常见成员

       

    class Program
    {
        static void Main(string[] args)
        {
            
            Type t1 = typeof(Parent);
            MethodInfo[] methods = t1.GetMethods();  //GetMethods方法,获取对象所有的方法
            foreach (MethodInfo method in methods)
            {
                Console.WriteLine(method);
            }
            MethodInfo m1 = t1.GetMethod("SayHi", new Type[0]);//获取指定无参方法
            MethodInfo m2 = t1.GetMethod("SayHi", new Type[] { typeof(string)});//获取指定有参方法,在后面指定参数类型
            PropertyInfo[] p1 = t1.GetProperties(); //获取所有的属性
            Console.ReadKey();
        } 
         
    }
     

    class Parent   
    {
        public void SayHi()
        {

        }

        public void SayHi(string name)
        {

        }

    }

4.  反射调用示例

   (1)

 

   (2)

        static void Main(string[] args)
        {
            Type type = typeof(Person);
            object obj = Activator.CreateInstance(type);//new Person()
            object obj2 = type.GetConstructor(new Type[0]).Invoke(new object[0]);
            PropertyInfo propertyInfo = type.GetProperty("Name");
            propertyInfo.SetValue(obj, "chen"); // obj.Name ="chen"

            MethodInfo methodInfo = type.GetMethod("SayHi", new Type[0]);
            methodInfo.Invoke(obj, new object[0]);//在obj指向的对象上滴哦啊用sayhi方法

            Console.WriteLine(type);


             Console.ReadKey();
        } 

   (3) 对象拷贝:

    class Person   
    {
        
        public int Age { get; set; }
        public string Name { get; set; }

        
        public void SayHi()
        {
            Console.WriteLine("大家好:我是{0},年龄{1}", Name, Age);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1.Name = "chen";
            p1.Age = 12;

            Person p2 = new Person();
            p2.Name = "chen1";
            p2.Age = 12;

            Person p3 = (Person)MyClone(p1);
            p3.SayHi();
            Console.ReadKey();

        } 
        //创建obj对象的拷贝
        static object MyClone(object obj)
        {
            Type type = obj.GetType(); //拿到对象的type
            object newobj = Activator.CreateInstance(type);//创建一个拷贝对象
            foreach (PropertyInfo  prop in type.GetProperties())//遍历每个属性
            {
                if(prop.CanRead&&prop.CanWrite)
                {
                    object value = prop.GetValue(obj); //获取obj对象的属性的值
                    prop.SetValue(newobj, value);
                }
            }
            return newobj;

        }
         

    }

    

原文地址:https://www.cnblogs.com/fuyouchen/p/9360980.html