PluginLoader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Windows;
  9. using System.Diagnostics;
  10. using AGV_WPF.Commands;
  11. using System.Collections.ObjectModel;
  12. namespace AGV_WPF.Plugin
  13. {
  14. public class PluginLoader
  15. {
  16. public static ArrayList pluginList = new ArrayList();
  17. public static ObservableCollection<ModuleInfo> moduleInfoList = new ObservableCollection<ModuleInfo>();
  18. public static void LoadAllPlugins()
  19. {
  20. //string pluginPath = System.IO.Path.Combine(Application.StartupPath, "plugins");
  21. string startPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
  22. string pluginPath = System.IO.Path.Combine(startPath, "plugins");
  23. if (!Directory.Exists(pluginPath))
  24. {
  25. Directory.CreateDirectory(pluginPath);
  26. }
  27. string[] files = Directory.GetFiles(pluginPath);
  28. foreach (string file in files)
  29. {
  30. if (file.ToLower().EndsWith(".dll"))
  31. {
  32. try
  33. {
  34. Assembly ab = Assembly.LoadFrom(file);
  35. Type[] types = ab.GetTypes();
  36. foreach (Type t in types)
  37. {
  38. //Console.WriteLine(t.FullName);
  39. if (t.GetInterface("Iplugin") != null)
  40. {
  41. Iplugin plugin = (Iplugin)ab.CreateInstance(t.FullName);
  42. pluginList.Add(plugin);//创建插件实例
  43. ModuleInfo info = new ModuleInfo();
  44. info.ModulePath = file;
  45. info.ModuleName = plugin.PluginName;
  46. info.ModuleSetCommand.ExecuteAction = new Action<object>(delegate(object o)
  47. {
  48. plugin.Fun(o);
  49. });
  50. info.ModuleOpenCommand.ExecuteAction = new Action<object>(delegate(object o)
  51. {
  52. Process p = new Process();
  53. p.StartInfo.FileName = System.IO.Path.GetDirectoryName(file);
  54. p.Start();
  55. });
  56. moduleInfoList.Add(info);
  57. }
  58. }
  59. }
  60. catch (System.Exception ex)
  61. {
  62. string fileName = System.IO.Path.GetFileName(file);
  63. string str = string.Format("插件{0}加载错误", fileName);
  64. throw new Exception(str);
  65. }
  66. }
  67. }
  68. Console.WriteLine(string.Format("Plugin Count:{0}", pluginList.Count));
  69. }
  70. }
  71. public class ModuleInfo
  72. {
  73. public string ModuleName { get; set; }
  74. public string ModulePath { get; set; }
  75. public DelegateCommand ModuleSetCommand { get; set; }
  76. public DelegateCommand ModuleOpenCommand { get; set; }
  77. public ModuleInfo()
  78. {
  79. ModuleSetCommand = new DelegateCommand();
  80. //ModuleSetCommand.ExecuteAction = new Action<object>(ModuleSet);
  81. ModuleOpenCommand = new DelegateCommand();
  82. /*ModuleOpenCommand.ExecuteAction = new Action<object>(delegate(object o)
  83. {
  84. MessageBox.Show("hello");
  85. });*/
  86. }
  87. }
  88. }