C#中反射的基础基础基础

时间:2019-09-27 10:07:37   收藏:0   阅读:90
    class Student
    {
        public int ID { get; set; }
        public string NAME { get; set; }
        public Student(int id, string name)
        {
            this.ID = id;
            this.NAME = name;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Type t = typeof(Student);//typeof(类) 取类的类型 并且存储在Type类型的t变量(其实是把类的类型的引用存在t中)
            //t stu1 = new t();这样是不对的
            object o = Activator.CreateInstance(t, 1, "One");//通过取到的t创建一个实例(包含非默认构造函数),CreateInstance创建的实例都是Object类型
            //o.Name;这种是不存在的,类型因为已经丢失了
            //o stu2 = new o();这样是不对的
            Student stu3 = (Student)o;
            Student stu4 = o as Student;
            stu3.ID = 3;
            stu3.NAME = "Three";
            stu4.ID = 4;
            stu4.NAME = "Four";
        }
    }

原文:https://www.cnblogs.com/maomaodesu/p/11595667.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!