Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

丁松杰 / Pole

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • Pole
  • src
  • Pole.Core
  • Utils
  • AssemblyHelper.cs
Find file
BlameHistoryPermalink
  • dingsongjie's avatar
    优化 字段名称 · d4fa1681
    dingsongjie committed 5 years ago
    d4fa1681
AssemblyHelper.cs 1.22 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;

namespace Pole.Core.Utils
{
    public class AssemblyHelper
    {
        private static IList<Assembly> assemblies;
        public static IList<Assembly> GetAssemblies(ILogger logger = default)
        {
            if (assemblies != null)
            {
                return assemblies;
            }
            var libs = DependencyContext.Default.CompileLibraries.Where(lib => !lib.Serviceable && !lib.Name.StartsWith("Microsoft") && !lib.Name.StartsWith("System"));
            assemblies = libs.Select(lib =>
              {
                  try
                  {
                      return AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(lib.Name));
                  }
                  catch (Exception ex)
                  {
                      if (logger != default)
                          logger.LogWarning(ex, ex.Message);
                      return default;
                  }
              }).Where(assembly => assembly != default).ToList();
            return assemblies;
        }
    }
}