Modified by D 的博客

.net 获取项目依赖的所有程序集

  1. AppDomain.CurrentDomain.GetAssemblies(),此办法不能获取到所有依赖的程序集,因为.Net有延迟加载程序集机制

  2. 适用于传统ASP.NET项目,System.Web.Compilation.BuildManager.GetReferencedAssemblies();

  3. 适用于ASP.NET Core项目,DependencyContext.Default.CompileLibraries

var dlls = DependencyContext.Default.CompileLibraries
    .SelectMany(x => x.ResolveReferencePaths())
    .Distinct()
    .Where(x => x.Contains(Directory.GetCurrentDirectory()))
    .ToList();
foreach (var item in dlls)
{
    try
    {
        AssemblyLoadContext.Default.LoadFromAssemblyPath(item);
    }
    catch (System.IO.FileLoadException loadEx)
    {
    } // The Assembly has already been loaded.
    catch (BadImageFormatException imgEx)
    {
    } // If a BadImageFormatException exception is thrown, the file is not an assembly.
    catch (Exception ex)
    {
    }
}

发表评论

评论列表,共 0 条评论

    暂无评论