反射_程序集_额外补充

来源:互联网 发布:mongodb删除数据 编辑:程序博客网 时间:2024/05/18 15:08

反射_程序集_额外补充<18/9/2017>

using System;using System.Collections.Generic;using System.Linq;using System.Reflection;//反射需要引用命名空间using System.Text;using System.Threading.Tasks;namespace 反射{    class Program    {        static void Main(string[] args)        {            Type t = Type.GetType("反射.Animal");            Type t1 = typeof(Animal);            Console.WriteLine(t.Equals(t1));//返回为true,返回对象是一样的            ConstructorInfo info = t.GetConstructor(new Type[] { typeof(int), typeof(string) });            object o = info.Invoke(new object[] { 1, "abc" });            //上面两句代码等同于下面这行代码            object o1 = Activator.CreateInstance(t, new object[] { 1, "adc" });            //            MethodInfo mi = t.GetMethod("Run");            mi.Invoke(o1, null);            MethodInfo mm = t.GetMethod("Sing");            mm.Invoke(null, null);            //1.静态方法可以传空执行,非静态方法必须有一个目标对象,如上            //2.可以返回一个结果,如下            MethodInfo mm1 = t.GetMethod("Sing2");            object sss = mm1.Invoke(null,null);            Console.WriteLine(sss);            PropertyInfo[] pies= t.GetProperties();            pies[0].SetValue(o1, 1001);            Console.WriteLine(((Animal)o1).Id);        }    }    public class Animal    {        public int Id { get; set; }            public int a;        public string b;        public Animal(int a,string b)        {            this.a = a;            this.b = b;        }        public Animal()        {        }        public void Run()        {            Console.WriteLine("123123213213");        }        public static void Sing()        {            Console.WriteLine("kkkkkkkkkkkk");        }        public static string Sing2()        {            return "xxxxxx";        }    }    public class Cat : Animal    {        public int ba;        public string bb;        public Cat(int a,string b)        {            this.ba = a;            this.bb = b;        }    }}


原创粉丝点击