This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
/
DependencyContext.cs
112 lines (95 loc) · 3.59 KB
/
DependencyContext.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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);
}
}
}
}