AssemblyFileListPlugInSource.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Abp.Collections.Extensions;
  6. using Abp.Modules;
  7. namespace Abp.PlugIns
  8. {
  9. //TODO: This class is similar to FolderPlugInSource. Create an abstract base class for them.
  10. public class AssemblyFileListPlugInSource : IPlugInSource
  11. {
  12. public string[] FilePaths { get; }
  13. private readonly Lazy<List<Assembly>> _assemblies;
  14. public AssemblyFileListPlugInSource(params string[] filePaths)
  15. {
  16. FilePaths = filePaths ?? new string[0];
  17. _assemblies = new Lazy<List<Assembly>>(LoadAssemblies, true);
  18. }
  19. public List<Assembly> GetAssemblies()
  20. {
  21. return _assemblies.Value;
  22. }
  23. public List<Type> GetModules()
  24. {
  25. var modules = new List<Type>();
  26. foreach (var assembly in GetAssemblies())
  27. {
  28. try
  29. {
  30. foreach (var type in assembly.GetTypes())
  31. {
  32. if (AbpModule.IsAbpModule(type))
  33. {
  34. modules.AddIfNotContains(type);
  35. }
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. throw new AbpInitializationException("Could not get module types from assembly: " + assembly.FullName, ex);
  41. }
  42. }
  43. return modules;
  44. }
  45. private List<Assembly> LoadAssemblies()
  46. {
  47. return FilePaths.Select(
  48. Assembly.LoadFile //TODO: Use AssemblyLoadContext.Default.LoadFromAssemblyPath instead?
  49. ).ToList();
  50. }
  51. }
  52. }