diff options
author | Atsushi Kanamori <AtsushiKan@users.noreply.github.com> | 2017-08-24 23:15:42 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-24 23:15:42 +0300 |
commit | bdeb76e6f9ec9d082c899dc2f6bde9b0aa7eeb65 (patch) | |
tree | 68d91e6900fbb3c906a883967a41529926c8501a /src/System.Private.Reflection.Core | |
parent | 67477abe2308d3b7238938444b6494a18ea3a07a (diff) |
Make Assembly.Load() throw same exception as CoreCLR (#4376)
When an assembly matching the simple name is found
but the version is wrong, Assembly.Load throws
a FileLoadException with a ref-def mismatch string.
This change makes Project N match that behavior as
well as separating the notion of a preferred exception
(the most informative exception to throw if no
matching candidate has found later in the search)
and the actual exception (used to signal that load
as a whole has failed.)
Diffstat (limited to 'src/System.Private.Reflection.Core')
-rw-r--r-- | src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs index 19201f5a1..fd9603606 100644 --- a/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs +++ b/src/System.Private.Reflection.Core/src/System/Reflection/Runtime/General/Dispensers.cs @@ -67,20 +67,30 @@ namespace System.Reflection.Runtime.Assemblies /// </summary> internal static RuntimeAssembly GetRuntimeAssemblyIfExists(RuntimeAssemblyName assemblyRefName) { - return s_assemblyRefNameToAssemblyDispenser.GetOrAdd(assemblyRefName); + object runtimeAssemblyOrException = s_assemblyRefNameToAssemblyDispenser.GetOrAdd(assemblyRefName); + if (runtimeAssemblyOrException is RuntimeAssembly runtimeAssembly) + return runtimeAssembly; + return null; } internal static Exception TryGetRuntimeAssembly(RuntimeAssemblyName assemblyRefName, out RuntimeAssembly result) { - result = GetRuntimeAssemblyIfExists(assemblyRefName); - if (result != null) + object runtimeAssemblyOrException = s_assemblyRefNameToAssemblyDispenser.GetOrAdd(assemblyRefName); + if (runtimeAssemblyOrException is RuntimeAssembly runtimeAssembly) + { + result = runtimeAssembly; return null; + } else - return new FileNotFoundException(SR.Format(SR.FileNotFound_AssemblyNotFound, assemblyRefName.FullName)); + { + result = null; + return (Exception)runtimeAssemblyOrException; + } } - private static readonly Dispenser<RuntimeAssemblyName, RuntimeAssembly> s_assemblyRefNameToAssemblyDispenser = - DispenserFactory.CreateDispenser<RuntimeAssemblyName, RuntimeAssembly>( + // The "object" here is either a RuntimeAssembly or an Exception. + private static readonly Dispenser<RuntimeAssemblyName, object> s_assemblyRefNameToAssemblyDispenser = + DispenserFactory.CreateDispenser<RuntimeAssemblyName, object>( DispenserScenario.AssemblyRefName_Assembly, delegate (RuntimeAssemblyName assemblyRefName) { @@ -88,7 +98,7 @@ namespace System.Reflection.Runtime.Assemblies AssemblyBindResult bindResult; Exception exception; if (!binder.Bind(assemblyRefName, out bindResult, out exception)) - return null; + return exception; return GetRuntimeAssembly(bindResult); } |