Permalink
Branch: master
Find file Copy path
6887ab5 Apr 27, 2017
1 contributor

Users who have contributed to this file

113 lines (95 sloc) 3.59 KB
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Microsoft.Extensions.DependencyModel
{
public class DependencyContext
{
#if !NETSTANDARD1_3
private static readonly Lazy<DependencyContext> _defaultContext = new Lazy<DependencyContext>(LoadDefault);
#endif
public DependencyContext(TargetInfo target,
CompilationOptions compilationOptions,
IEnumerable<CompilationLibrary> compileLibraries,
IEnumerable<RuntimeLibrary> runtimeLibraries,
IEnumerable<RuntimeFallbacks> runtimeGraph)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
if (compilationOptions == null)
{
throw new ArgumentNullException(nameof(compilationOptions));
}
if (compileLibraries == null)
{
throw new ArgumentNullException(nameof(compileLibraries));
}
if (runtimeLibraries == null)
{
throw new ArgumentNullException(nameof(runtimeLibraries));
}
if (runtimeGraph == null)
{
throw new ArgumentNullException(nameof(runtimeGraph));
}
Target = target;
CompilationOptions = compilationOptions;
CompileLibraries = compileLibraries.ToArray();
RuntimeLibraries = runtimeLibraries.ToArray();
RuntimeGraph = runtimeGraph.ToArray();
}
#if !NETSTANDARD1_3
public static DependencyContext Default => _defaultContext.Value;
#endif
public TargetInfo Target { get; }
public CompilationOptions CompilationOptions { get; }
public IReadOnlyList<CompilationLibrary> CompileLibraries { get; }
public IReadOnlyList<RuntimeLibrary> RuntimeLibraries { get; }
public IReadOnlyList<RuntimeFallbacks> RuntimeGraph { get; }
public DependencyContext Merge(DependencyContext other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
return new DependencyContext(
Target,
CompilationOptions,
CompileLibraries.Union(other.CompileLibraries, new LibraryMergeEqualityComparer<CompilationLibrary>()),
RuntimeLibraries.Union(other.RuntimeLibraries, new LibraryMergeEqualityComparer<RuntimeLibrary>()),
RuntimeGraph.Union(other.RuntimeGraph)
);
}
#if !NETSTANDARD1_3
private static DependencyContext LoadDefault()
{
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly == null)
{
return null;
}
return Load(entryAssembly);
}
public static DependencyContext Load(Assembly assembly)
{
return DependencyContextLoader.Default.Load(assembly);
}
#endif
private class LibraryMergeEqualityComparer<T> : IEqualityComparer<T> where T : Library
{
public bool Equals(T x, T y)
{
return StringComparer.OrdinalIgnoreCase.Equals(x.Name, y.Name);
}
public int GetHashCode(T obj)
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Name);
}
}
}
}