C#之反射
Assembly assembly = Assembly.Load("PeopleDal"); //获取程序集名称 Module[] modules = assembly.GetModules(); foreach (var item in modules) { Console.WriteLine(item.Name); } //获取程序集类名 Type[] types = assembly.GetTypes(); foreach (var item in types) { Console.WriteLine(item.Name); } //注意:程序集名称需要拷贝到当前项目的\bin\Debug目录下 //加载程序集,Load里面写程序集名称 Assembly assembly = Assembly.Load("PeopleDal"); //获取程序集实例,GetType里面写“命名空间”加上“.类名” Type type = assembly.GetType("PeopleDal.People"); //创建该类型的实例并类型转换 IHelper iHelper = Activator.CreateInstance(type) as IHelper; //调用该类型的方法 iHelper.SayHi(); //配置文件方法读取并创建实例、调用方法 string helperConfig = ConfigurationManager.AppSettings["Helper"]; Assembly assembly = Assembly.Load(helperConfig.Split(',')[0]); Type type = assembly.GetType(helperConfig.Split(',')[1]); IHelper iHelper = Activator.CreateInstance(type) as IHelper; iHelper.SayHi(); Console.ReadKey();