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";
}
}
C#中反射的基础基础基础