日期:2014-05-17 浏览次数:20450 次
void Application_Start(object sender, EventArgs e) { //在应用程序启动时运行的代码 Framework.PlugsHostService.Instance.LoadPlugs(); } ------------- private bool alreadyLoad = false; //查找所有的插件的路径 private static List<string> FindPlugin() { List<string> PluginPaths = new List<string>(); try { //获取当前程序的启动路径 string path = appLocation; //合并路径,指向插件所在的目录 path = Path.Combine(path, "Lib"); string[] fileNames = Directory.GetFiles(path, "*.dll"); foreach (string fileName in fileNames) { PluginPaths.Add(fileName); } } catch (Exception exp) { throw exp; } return PluginPaths; } public void LoadPlugs() { if (alreadyLoad) { return; } List<string> PluginPaths = FindPlugin(); PluginPaths = DeleteInvalidPlugin(PluginPaths); foreach (string path in PluginPaths) { try { //获得 文件名称 string asmFile = path; string asmName = System.IO.Path.GetFileNameWithoutExtension(asmFile); if (asmFile != string.Empty) { // 利用反射,构造DLL文件的实例 System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(asmFile); Type[] t = asm.GetExportedTypes(); foreach (Type type in t) { if (type.GetInterface(typeof(IPlugs).FullName) != null) { IPlugs iPlugs = (IPlugs)System.Activator.CreateInstance(type); // 在主程序中使用这个类的实例 //AppDomain.CurrentDomain.Load(asm.GetName()); //AppDomain.CurrentDomain.ExecuteAssembly(asmFile);//.ExecuteAssembly(asmFile); AppDomain.CurrentDomain.Load(asm.FullName, AppDomain.CurrentDomain.Evidence); //调试时,到这里就报错 } } } alreadyLoad = true; } catch (Exception ex) { throw ex; } } }