Created
March 31, 2017 09:53
-
-
Save andreaskoepf/b4b3837f3ced6197432957298e56647c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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