程序集---反射---动态创建对象

•Activator.CreateInstance(Type t)会动态调用类的无参构造函数创建一个对象,返回值就是创建的对象,如果类没有无参构造函数就会报错。
View Code
 1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             string filename = @"D:\我的文档\快盘\传智资料\班级资料\2011年2月28日班 \课上代码\Sln0410\测试程序集\bin\Debug\测试程序集.dll";
 6             //从文件中加载程序集
 7             Assembly asm = Assembly.LoadFile(filename);
 8             Type t1 = asm.GetExportedTypes()[0];
 9             object obj1 = Activator.CreateInstance(t1);//动态创建类型的对象
10             Console.WriteLine(obj1);
11 
12             //Person p = (Person)obj1;
13             Console.ReadKey();
14         }
15     }

 

 
posted @ 2013-03-17 22:47  Big.Eagle  阅读(99)  评论(0)    收藏  举报