using System;
using System.Text;
using System.Reflection;
namespace ReflectionDemo
{
class program
{
static void Main(string[] args)
{
string assemblyPath=@"D:\我的文档\Visual Studio 2008\Projects\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.exe";
string assemblyType="ReflectionDemo.Student";
Type studentType=Assembly.LoadFile(assemblyPath).GetType(assemblyType);
Person student=Activator.CreateInstance(studentType) as Person;
student.SayHi();//结果:这是一个学生!
}
}
class Person
{
public virtual void SayHi(){
Console.WriteLine("这是一个人!");
}
}
class Student:Person
{
public override void SayHi(){
Console.WriteLine("这是一个学生!");
}
}
}
反射小例子
最新推荐文章于 2024-03-16 09:00:00 发布