Skip to content

Instantly share code, notes, and snippets.

@andreaskoepf
Created March 31, 2017 09:53
Show Gist options
  • Save andreaskoepf/b4b3837f3ced6197432957298e56647c to your computer and use it in GitHub Desktop.
Save andreaskoepf/b4b3837f3ced6197432957298e56647c to your computer and use it in GitHub Desktop.
using Microsoft.Extensions.DependencyModel;
using System;
using System.Linq;
using System.Runtime.Loader;
using System.Reflection;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static IEnumerable<Assembly> GetCandidateAssemblies(params string[] tagAssemblies)
{
if (tagAssemblies == null)
throw new ArgumentNullException(nameof(tagAssemblies));
if (tagAssemblies.Length == 0)
throw new ArgumentException("At least one tag assembly name must be specified.", nameof(tagAssemblies));
var context = DependencyContext.Load(Assembly.GetEntryAssembly());
var loadContext = AssemblyLoadContext.Default;
var referenceAssemblies = new HashSet<string>(tagAssemblies, StringComparer.OrdinalIgnoreCase);
return context.RuntimeLibraries
.Where(x => x.Dependencies.Any(d => referenceAssemblies.Contains(d.Name)))
.SelectMany(x => x.GetDefaultAssemblyNames(context))
.Select(x => loadContext.LoadFromAssemblyName(x));
}
static void Main(string[] args)
{
var l = GetCandidateAssemblies("newtonsoft.json").ToList();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment