FolderPlugInSource.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using Abp.Collections.Extensions;
  6. using Abp.Modules;
  7. using Abp.Reflection;
  8. namespace Abp.PlugIns
  9. {
  10. public class FolderPlugInSource : IPlugInSource
  11. {
  12. public string Folder { get; }
  13. public SearchOption SearchOption { get; set; }
  14. private readonly Lazy<List<Assembly>> _assemblies;
  15. public FolderPlugInSource(string folder, SearchOption searchOption = SearchOption.TopDirectoryOnly)
  16. {
  17. Folder = folder;
  18. SearchOption = searchOption;
  19. _assemblies = new Lazy<List<Assembly>>(LoadAssemblies, true);
  20. }
  21. public List<Assembly> GetAssemblies()
  22. {
  23. return _assemblies.Value;
  24. }
  25. public List<Type> GetModules()
  26. {
  27. var modules = new List<Type>();
  28. foreach (var assembly in GetAssemblies())
  29. {
  30. try
  31. {
  32. foreach (var type in assembly.GetTypes())
  33. {
  34. if (AbpModule.IsAbpModule(type))
  35. {
  36. modules.AddIfNotContains(type);
  37. }
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. throw new AbpInitializationException("Could not get module types from assembly: " + assembly.FullName, ex);
  43. }
  44. }
  45. return modules;
  46. }
  47. private List<Assembly> LoadAssemblies()
  48. {
  49. return AssemblyHelper.GetAllAssembliesInFolder(Folder, SearchOption);
  50. }
  51. }
  52. }