AssemblyUtils.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using Microsoft.Extensions.DependencyModel;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Infrastructure.Helper
  9. {
  10. public static class AssemblyUtils
  11. {
  12. /// <summary>
  13. /// 获取应用中的所有程序集
  14. /// </summary>
  15. /// <returns></returns>
  16. public static IEnumerable<Assembly> GetAssemblies()
  17. {
  18. var compilationLibrary = DependencyContext.Default
  19. .CompileLibraries
  20. .Where(x => !x.Serviceable && x.Type == "project")
  21. .ToList();
  22. return compilationLibrary.Select(p => Assembly.Load(new AssemblyName(p.Name)));
  23. }
  24. /// <summary>
  25. /// 获取应用中的所有Type
  26. /// </summary>
  27. /// <returns></returns>
  28. public static IEnumerable<Type> GetAllTypes()
  29. {
  30. var assemblies = GetAssemblies();
  31. return assemblies.SelectMany(p => p.GetTypes());
  32. }
  33. }
  34. }