-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathMetadataLoadContext.CoreAssembly.cs
More file actions
96 lines (84 loc) · 3.75 KB
/
MetadataLoadContext.CoreAssembly.cs
File metadata and controls
96 lines (84 loc) · 3.75 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.IO;
using System.Reflection.TypeLoading;
namespace System.Reflection
{
public sealed partial class MetadataLoadContext
{
private static readonly string[] s_CoreNames = { "mscorlib", "System.Runtime", "netstandard" };
// Cache loaded coreAssembly and core types.
internal RoAssembly LoadCoreAssembly(string? coreAssemblyName)
{
Debug.Assert(_coreAssembly == null);
RoAssembly? coreAssembly;
Exception? e;
if (coreAssemblyName == null)
{
coreAssembly = TryGetDefaultCoreAssembly(out e);
}
else
{
RoAssemblyName roAssemblyName = new AssemblyName(coreAssemblyName).ToRoAssemblyName();
coreAssembly = TryResolveAssembly(roAssemblyName, out e);
}
if (coreAssembly == null)
{
throw e!;
}
return coreAssembly;
}
private RoAssembly? TryGetDefaultCoreAssembly(out Exception? e)
{
foreach (string coreName in s_CoreNames)
{
RoAssemblyName roAssemblyName = new AssemblyName(coreName).ToRoAssemblyName();
RoAssembly? roAssembly = TryResolveAssembly(roAssemblyName, out e);
// Stop on the first core assembly we find
if (roAssembly != null)
{
e = null;
return roAssembly;
}
}
e = new FileNotFoundException(SR.UnableToDetermineCoreAssembly);
return null;
}
private readonly RoAssembly _coreAssembly;
/// <summary>
/// Returns a lazily created and cached Type instance corresponding to the indicated core type. This method throws
/// if the core assembly name wasn't supplied, the core assembly could not be loaded for some reason or if the specified
/// type does not exist in the core assembly.
/// </summary>
internal RoType GetCoreType(CoreType coreType)
{
CoreTypes coreTypes = GetAllFoundCoreTypes();
RoType? t = TryGetCoreType(coreType);
return t ?? throw coreTypes.GetException(coreType)!;
}
/// <summary>
/// Returns a lazily created and cached Type instance corresponding to the indicated core type. This method returns null
/// if the core assembly name wasn't supplied, the core assembly could not be loaded for some reason or if the specified
/// type does not exist in the core assembly.
/// </summary>
internal RoType? TryGetCoreType(CoreType coreType)
{
CoreTypes coreTypes = GetAllFoundCoreTypes();
return coreTypes[coreType];
}
/// <summary>
/// Returns a cached array containing the resolved CoreTypes, indexed by the CoreType enum cast to an int.
/// If the core assembly was not specified, not locatable or if one or more core types aren't present in the core assembly,
/// the corresponding elements will be null.
/// </summary>
internal CoreTypes GetAllFoundCoreTypes() => _coreTypes;
private readonly CoreTypes _coreTypes;
//
// Seriously, ugh - the default binder for Reflection has a dependency on checking types for equality with System.Object - for that
// one reason, we have to instance it per MetadataLoadContext.
//
internal Binder GetDefaultBinder() => _lazyDefaultBinder ??= new DefaultBinder(this);
private Binder? _lazyDefaultBinder;
}
}