using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using System.IO; using System.Reflection; using System.Windows; using System.Diagnostics; using AGV_WPF.Commands; using System.Collections.ObjectModel; namespace AGV_WPF.Plugin { public class PluginLoader { public static ArrayList pluginList = new ArrayList(); public static ObservableCollection moduleInfoList = new ObservableCollection(); public static void LoadAllPlugins() { //string pluginPath = System.IO.Path.Combine(Application.StartupPath, "plugins"); string startPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); string pluginPath = System.IO.Path.Combine(startPath, "plugins"); if (!Directory.Exists(pluginPath)) { Directory.CreateDirectory(pluginPath); } string[] files = Directory.GetFiles(pluginPath); foreach (string file in files) { if (file.ToLower().EndsWith(".dll")) { try { Assembly ab = Assembly.LoadFrom(file); Type[] types = ab.GetTypes(); foreach (Type t in types) { //Console.WriteLine(t.FullName); if (t.GetInterface("Iplugin") != null) { Iplugin plugin = (Iplugin)ab.CreateInstance(t.FullName); pluginList.Add(plugin);//创建插件实例 ModuleInfo info = new ModuleInfo(); info.ModulePath = file; info.ModuleName = plugin.PluginName; info.ModuleSetCommand.ExecuteAction = new Action(delegate(object o) { plugin.Fun(o); }); info.ModuleOpenCommand.ExecuteAction = new Action(delegate(object o) { Process p = new Process(); p.StartInfo.FileName = System.IO.Path.GetDirectoryName(file); p.Start(); }); moduleInfoList.Add(info); } } } catch (System.Exception ex) { string fileName = System.IO.Path.GetFileName(file); string str = string.Format("插件{0}加载错误", fileName); throw new Exception(str); } } } Console.WriteLine(string.Format("Plugin Count:{0}", pluginList.Count)); } } public class ModuleInfo { public string ModuleName { get; set; } public string ModulePath { get; set; } public DelegateCommand ModuleSetCommand { get; set; } public DelegateCommand ModuleOpenCommand { get; set; } public ModuleInfo() { ModuleSetCommand = new DelegateCommand(); //ModuleSetCommand.ExecuteAction = new Action(ModuleSet); ModuleOpenCommand = new DelegateCommand(); /*ModuleOpenCommand.ExecuteAction = new Action(delegate(object o) { MessageBox.Show("hello"); });*/ } } }