diff --git a/src/Scripting/Core/CoreLightup.cs b/src/Scripting/Core/CoreLightup.cs deleted file mode 100644 index 7af9ded1629c8..0000000000000 --- a/src/Scripting/Core/CoreLightup.cs +++ /dev/null @@ -1,127 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#nullable disable - -using System; -using System.Diagnostics; -using System.Globalization; -using System.IO; -using System.Reflection; - -namespace Roslyn.Utilities -{ - /// - /// This type contains the light up scenarios for various platform and runtimes. Any function - /// in this type can, and is expected to, fail on various platforms. These are light up scenarios - /// only. - /// - internal static class CoreLightup - { - internal static class Desktop - { - private static class _Assembly - { - internal static readonly Type Type = typeof(Assembly); - - internal static readonly Func get_GlobalAssemblyCache = Type - .GetTypeInfo() - .GetDeclaredMethod("get_GlobalAssemblyCache") - .CreateDelegate>(); - } - - private static class _ResolveEventArgs - { - internal static readonly Type Type = ReflectionUtilities.TryGetType("System.ResolveEventArgs"); - - internal static readonly MethodInfo get_Name = Type - .GetTypeInfo() - .GetDeclaredMethod("get_Name"); - - internal static readonly MethodInfo get_RequestingAssembly = Type - .GetTypeInfo() - .GetDeclaredMethod("get_RequestingAssembly"); - } - - private static class _AppDomain - { - internal static readonly Type Type = ReflectionUtilities.TryGetType("System.AppDomain"); - internal static readonly Type ResolveEventHandlerType = ReflectionUtilities.TryGetType("System.ResolveEventHandler"); - - internal static readonly MethodInfo get_CurrentDomain = Type - .GetTypeInfo() - .GetDeclaredMethod("get_CurrentDomain"); - - internal static readonly MethodInfo add_AssemblyResolve = Type - .GetTypeInfo() - .GetDeclaredMethod("add_AssemblyResolve", ResolveEventHandlerType); - - internal static readonly MethodInfo remove_AssemblyResolve = Type - .GetTypeInfo() - .GetDeclaredMethod("remove_AssemblyResolve", ResolveEventHandlerType); - } - - internal static bool IsAssemblyFromGlobalAssemblyCache(Assembly assembly) - { - if (_Assembly.get_GlobalAssemblyCache == null) - { - throw new PlatformNotSupportedException(); - } - - return _Assembly.get_GlobalAssemblyCache(assembly); - } - - private sealed class AssemblyResolveWrapper - { - private readonly Func _handler; - private static readonly MethodInfo s_stubInfo = typeof(AssemblyResolveWrapper).GetTypeInfo().GetDeclaredMethod("Stub"); - - public AssemblyResolveWrapper(Func handler) - { - _handler = handler; - } - -#pragma warning disable IDE0051 // Remove unused private members - Reflection (s_stubInfo) -#pragma warning disable IDE0060 // Remove unused parameter - private Assembly Stub(object sender, object resolveEventArgs) -#pragma warning restore IDE0051 // Remove unused private members -#pragma warning restore IDE0060 // Remove unused parameter - { - var name = (string)_ResolveEventArgs.get_Name.Invoke(resolveEventArgs, Array.Empty()); - var requestingAssembly = (Assembly)_ResolveEventArgs.get_RequestingAssembly.Invoke(resolveEventArgs, Array.Empty()); - - return _handler(name, requestingAssembly); - } - - public object GetHandler() - { - return s_stubInfo.CreateDelegate(_AppDomain.ResolveEventHandlerType, this); - } - } - - internal static void GetOrRemoveAssemblyResolveHandler(Func handler, MethodInfo handlerOperation) - { - if (_AppDomain.add_AssemblyResolve == null) - { - throw new PlatformNotSupportedException(); - } - - var currentAppDomain = AppDomain.CurrentDomain; - object resolveEventHandler = new AssemblyResolveWrapper(handler).GetHandler(); - - handlerOperation.Invoke(currentAppDomain, [resolveEventHandler]); - } - - internal static void AddAssemblyResolveHandler(Func handler) - { - GetOrRemoveAssemblyResolveHandler(handler, _AppDomain.add_AssemblyResolve); - } - - internal static void RemoveAssemblyResolveHandler(Func handler) - { - GetOrRemoveAssemblyResolveHandler(handler, _AppDomain.remove_AssemblyResolve); - } - } - } -} diff --git a/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyAndLocation.cs b/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyAndLocation.cs index e67168a9500b2..9fb3f7022c9b6 100644 --- a/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyAndLocation.cs +++ b/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyAndLocation.cs @@ -2,42 +2,14 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - -using System; -using System.Diagnostics; using System.Reflection; -using Roslyn.Utilities; - -namespace Microsoft.CodeAnalysis.Scripting.Hosting -{ - internal readonly struct AssemblyAndLocation : IEquatable - { - public Assembly Assembly { get; } - public string Location { get; } - public bool GlobalAssemblyCache { get; } - - internal AssemblyAndLocation(Assembly assembly, string location, bool fromGac) - { - Debug.Assert(assembly != null && location != null); - Assembly = assembly; - Location = location; - GlobalAssemblyCache = fromGac; - } +namespace Microsoft.CodeAnalysis.Scripting.Hosting; - public bool IsDefault => Assembly == null; - - public bool Equals(AssemblyAndLocation other) - => Assembly == other.Assembly && Location == other.Location && GlobalAssemblyCache == other.GlobalAssemblyCache; - - public override int GetHashCode() - => Hash.Combine(Assembly, Hash.Combine(Location, Hash.Combine(GlobalAssemblyCache, 0))); - - public override bool Equals(object obj) - => obj is AssemblyAndLocation && Equals((AssemblyAndLocation)obj); +internal readonly record struct AssemblyAndLocation(Assembly Assembly, string Location, bool GlobalAssemblyCache) +{ + public bool IsDefault => Assembly == null; - public override string ToString() - => Assembly + " @ " + (GlobalAssemblyCache ? "" : Location); - } + public override string ToString() + => Assembly + " @ " + (GlobalAssemblyCache ? "" : Location); } diff --git a/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoadResult.cs b/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoadResult.cs index 75dd78620b83d..7589e3b503749 100644 --- a/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoadResult.cs +++ b/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoadResult.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - namespace Microsoft.CodeAnalysis.Scripting.Hosting { /// diff --git a/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoaderImpl.cs b/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoaderImpl.cs index d7d8401ecd2b8..18cd4541c49e7 100644 --- a/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoaderImpl.cs +++ b/src/Scripting/Core/Hosting/AssemblyLoader/AssemblyLoaderImpl.cs @@ -2,43 +2,22 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.IO; using System.Reflection; -using System.Runtime.CompilerServices; -using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Scripting.Hosting { - internal abstract class AssemblyLoaderImpl : IDisposable + internal abstract class AssemblyLoaderImpl(InteractiveAssemblyLoader loader) : IDisposable { - internal readonly InteractiveAssemblyLoader Loader; - - protected AssemblyLoaderImpl(InteractiveAssemblyLoader loader) - { - Loader = loader; - } + internal readonly InteractiveAssemblyLoader Loader = loader; public static AssemblyLoaderImpl Create(InteractiveAssemblyLoader loader) - { - if (CoreClrShim.AssemblyLoadContext.Type != null) - { - return CreateCoreImpl(loader); - } - else - { - return new DesktopAssemblyLoaderImpl(loader); - } - } - - // NoInlining to avoid loading AssemblyLoadContext if not available. - [MethodImpl(MethodImplOptions.NoInlining)] - private static AssemblyLoaderImpl CreateCoreImpl(InteractiveAssemblyLoader loader) - { - return new CoreAssemblyLoaderImpl(loader); - } +#if NET + => new CoreAssemblyLoaderImpl(loader); +#else + => new DesktopAssemblyLoaderImpl(loader); +#endif public abstract Assembly LoadFromStream(Stream peStream, Stream pdbStream); public abstract AssemblyAndLocation LoadFromPath(string path); diff --git a/src/Scripting/Core/Hosting/AssemblyLoader/CoreAssemblyLoaderImpl.cs b/src/Scripting/Core/Hosting/AssemblyLoader/CoreAssemblyLoaderImpl.cs index baeb4f0e2c690..e539846aded3f 100644 --- a/src/Scripting/Core/Hosting/AssemblyLoader/CoreAssemblyLoaderImpl.cs +++ b/src/Scripting/Core/Hosting/AssemblyLoader/CoreAssemblyLoaderImpl.cs @@ -2,10 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable +#if NET -using System; -using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.Loader; @@ -34,7 +32,7 @@ public override AssemblyAndLocation LoadFromPath(string path) // but there is no need to reuse contexts. var assembly = new LoadContext(Loader, Path.GetDirectoryName(path)).LoadFromAssemblyPath(path); - return new AssemblyAndLocation(assembly, path, fromGac: false); + return new AssemblyAndLocation(assembly, path, GlobalAssemblyCache: false); } public override void Dispose() @@ -44,15 +42,13 @@ public override void Dispose() private sealed class LoadContext : AssemblyLoadContext { - private readonly string _loadDirectoryOpt; + private readonly string? _loadDirectory; private readonly InteractiveAssemblyLoader _loader; - internal LoadContext(InteractiveAssemblyLoader loader, string loadDirectoryOpt) + internal LoadContext(InteractiveAssemblyLoader loader, string? loadDirectory) { - Debug.Assert(loader != null); - _loader = loader; - _loadDirectoryOpt = loadDirectoryOpt; + _loadDirectory = loadDirectory; // CoreCLR resolves assemblies in steps: // @@ -68,10 +64,11 @@ internal LoadContext(InteractiveAssemblyLoader loader, string loadDirectoryOpt) // This order is necessary to avoid loading assemblies twice (by the host App and by interactive loader). Resolving += (_, assemblyName) => - _loader.ResolveAssembly(AssemblyIdentity.FromAssemblyReference(assemblyName), _loadDirectoryOpt); + _loader.ResolveAssembly(AssemblyIdentity.FromAssemblyReference(assemblyName), _loadDirectory); } - protected override Assembly Load(AssemblyName assemblyName) => null; + protected override Assembly? Load(AssemblyName assemblyName) => null; } } } +#endif diff --git a/src/Scripting/Core/Hosting/AssemblyLoader/DesktopAssemblyLoaderImpl.cs b/src/Scripting/Core/Hosting/AssemblyLoader/DesktopAssemblyLoaderImpl.cs index 3fedb5a7cd172..e84fb9395ec6a 100644 --- a/src/Scripting/Core/Hosting/AssemblyLoader/DesktopAssemblyLoaderImpl.cs +++ b/src/Scripting/Core/Hosting/AssemblyLoader/DesktopAssemblyLoaderImpl.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable +#if !NET using System; using System.IO; @@ -13,31 +13,28 @@ namespace Microsoft.CodeAnalysis.Scripting.Hosting { internal sealed class DesktopAssemblyLoaderImpl : AssemblyLoaderImpl { - private readonly Func _assemblyResolveHandlerOpt; - public DesktopAssemblyLoaderImpl(InteractiveAssemblyLoader loader) : base(loader) { - _assemblyResolveHandlerOpt = loader.ResolveAssembly; - CoreLightup.Desktop.AddAssemblyResolveHandler(_assemblyResolveHandlerOpt); + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; } public override void Dispose() { - if (_assemblyResolveHandlerOpt != null) - { - CoreLightup.Desktop.RemoveAssemblyResolveHandler(_assemblyResolveHandlerOpt); - } + AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; } + private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) + => Loader.ResolveAssembly(args.Name, args.RequestingAssembly); + public override Assembly LoadFromStream(Stream peStream, Stream pdbStream) { - byte[] peImage = new byte[peStream.Length]; + var peImage = new byte[peStream.Length]; peStream.TryReadAll(peImage, 0, peImage.Length); if (pdbStream != null) { - byte[] pdbImage = new byte[pdbStream.Length]; + var pdbImage = new byte[pdbStream.Length]; pdbStream.TryReadAll(pdbImage, 0, pdbImage.Length); return Assembly.Load(peImage, pdbImage); @@ -52,9 +49,8 @@ public override AssemblyAndLocation LoadFromPath(string path) // Assembly.LoadFile(string) automatically redirects to GAC if the assembly has a strong name and there is an equivalent assembly in GAC. var assembly = Assembly.LoadFile(path); - var location = assembly.Location; - var fromGac = CoreLightup.Desktop.IsAssemblyFromGlobalAssemblyCache(assembly); - return new AssemblyAndLocation(assembly, location, fromGac); + return new AssemblyAndLocation(assembly, assembly.Location, assembly.GlobalAssemblyCache); } } } +#endif diff --git a/src/Scripting/Core/Hosting/AssemblyLoader/InteractiveAssemblyLoader.cs b/src/Scripting/Core/Hosting/AssemblyLoader/InteractiveAssemblyLoader.cs index bff59c9770b35..6d55c480fdcad 100644 --- a/src/Scripting/Core/Hosting/AssemblyLoader/InteractiveAssemblyLoader.cs +++ b/src/Scripting/Core/Hosting/AssemblyLoader/InteractiveAssemblyLoader.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics; @@ -13,7 +11,6 @@ using System.Reflection; using System.Reflection.Metadata; using System.Reflection.PortableExecutable; -using System.Runtime.CompilerServices; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Scripting.Hosting @@ -34,12 +31,12 @@ private class LoadedAssembly /// The original path of the assembly before it was shadow-copied. /// For GAC'd assemblies, this is equal to Assembly.Location no matter what path was used to load them. /// - public string OriginalPath { get; set; } + public string? OriginalPath { get; set; } } private readonly AssemblyLoaderImpl _runtimeAssemblyLoader; - private readonly MetadataShadowCopyProvider _shadowCopyProvider; + private readonly MetadataShadowCopyProvider? _shadowCopyProvider; // Synchronizes assembly reference tracking. // Needs to be thread-safe since assembly loading may be triggered at any time by CLR type loader. @@ -66,8 +63,6 @@ private readonly struct AssemblyIdentityAndLocation public AssemblyIdentityAndLocation(AssemblyIdentity identity, string location) { - Debug.Assert(identity != null && location != null); - Identity = identity; Location = location; } @@ -76,26 +71,18 @@ public AssemblyIdentityAndLocation(AssemblyIdentity identity, string location) } [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] - private readonly struct LoadedAssemblyInfo + private readonly struct LoadedAssemblyInfo(Assembly assembly, AssemblyIdentity identity, string? location) { - public readonly Assembly Assembly; - public readonly AssemblyIdentity Identity; - public readonly string LocationOpt; - - public LoadedAssemblyInfo(Assembly assembly, AssemblyIdentity identity, string locationOpt) - { - Debug.Assert(assembly != null && identity != null); - - Assembly = assembly; - Identity = identity; - LocationOpt = locationOpt; - } + public readonly Assembly Assembly = assembly; + public readonly AssemblyIdentity Identity = identity; + public readonly string? Location = location; public bool IsDefault => Assembly == null; - private string GetDebuggerDisplay() => IsDefault ? "uninitialized" : Identity.GetDisplayName() + (LocationOpt != null ? " @ " + LocationOpt : ""); + + private string GetDebuggerDisplay() => IsDefault ? "uninitialized" : Identity.GetDisplayName() + (Location != null ? " @ " + Location : ""); } - public InteractiveAssemblyLoader(MetadataShadowCopyProvider shadowCopyProvider = null) + public InteractiveAssemblyLoader(MetadataShadowCopyProvider? shadowCopyProvider = null) { _shadowCopyProvider = shadowCopyProvider; @@ -121,7 +108,7 @@ internal Assembly LoadAssemblyFromStream(Stream peStream, Stream pdbStream) private AssemblyAndLocation Load(string reference) { - MetadataShadowCopy copy = null; + MetadataShadowCopy? copy = null; try { if (_shadowCopyProvider != null) @@ -144,7 +131,7 @@ private AssemblyAndLocation Load(string reference) } catch (FileNotFoundException) { - return default(AssemblyAndLocation); + return default; } finally { @@ -201,17 +188,16 @@ public void RegisterDependency(Assembly dependency) lock (_referencesLock) { - RegisterLoadedAssemblySimpleNameNoLock(dependency, locationOpt: null); + RegisterLoadedAssemblySimpleNameNoLock(dependency, location: null); } } - private void RegisterLoadedAssemblySimpleNameNoLock(Assembly assembly, string locationOpt) + private void RegisterLoadedAssemblySimpleNameNoLock(Assembly assembly, string? location) { var identity = AssemblyIdentity.FromAssemblyDefinition(assembly); - var info = new LoadedAssemblyInfo(assembly, identity, locationOpt); + var info = new LoadedAssemblyInfo(assembly, identity, location); - List existingInfos; - if (_loadedAssembliesBySimpleName.TryGetValue(identity.Name, out existingInfos)) + if (_loadedAssembliesBySimpleName.TryGetValue(identity.Name, out var existingInfos)) { existingInfos.Add(info); } @@ -223,9 +209,8 @@ private void RegisterLoadedAssemblySimpleNameNoLock(Assembly assembly, string lo private void RegisterDependencyNoLock(AssemblyIdentityAndLocation dependency) { - List sameSimpleNameAssemblyIdentities; string simpleName = dependency.Identity.Name; - if (_dependenciesWithLocationBySimpleName.TryGetValue(simpleName, out sameSimpleNameAssemblyIdentities)) + if (_dependenciesWithLocationBySimpleName.TryGetValue(simpleName, out var sameSimpleNameAssemblyIdentities)) { sameSimpleNameAssemblyIdentities.Add(dependency); } @@ -235,53 +220,50 @@ private void RegisterDependencyNoLock(AssemblyIdentityAndLocation dependency) } } - internal Assembly ResolveAssembly(string assemblyDisplayName, Assembly requestingAssemblyOpt) + internal Assembly? ResolveAssembly(string assemblyDisplayName, Assembly? requestingAssembly) { - AssemblyIdentity identity; - if (!AssemblyIdentity.TryParseDisplayName(assemblyDisplayName, out identity)) + if (!AssemblyIdentity.TryParseDisplayName(assemblyDisplayName, out var identity)) { return null; } - string loadDirectoryOpt; + string? loadDirectory; lock (_referencesLock) { - LoadedAssembly loadedAssembly; - if (requestingAssemblyOpt != null && - _assembliesLoadedFromLocation.TryGetValue(requestingAssemblyOpt, out loadedAssembly)) + if (requestingAssembly != null && + _assembliesLoadedFromLocation.TryGetValue(requestingAssembly, out var loadedAssembly)) { - loadDirectoryOpt = Path.GetDirectoryName(loadedAssembly.OriginalPath); + loadDirectory = Path.GetDirectoryName(loadedAssembly.OriginalPath); } else { - loadDirectoryOpt = null; + loadDirectory = null; } } - return ResolveAssembly(identity, loadDirectoryOpt); + return ResolveAssembly(identity, loadDirectory); } - internal Assembly ResolveAssembly(AssemblyIdentity identity, string loadDirectoryOpt) + internal Assembly? ResolveAssembly(AssemblyIdentity identity, string? loadDirectory) { // if the referring assembly is already loaded by our loader, load from its directory: - if (loadDirectoryOpt != null) + if (loadDirectory != null) { - Assembly assembly; + Assembly? assembly; var conflictingLoadedAssemblyOpt = default(LoadedAssemblyInfo); var loadedAssemblyWithEqualNameAndVersionOpt = default(LoadedAssemblyInfo); lock (_referencesLock) { // Has the file already been loaded? - assembly = TryGetAssemblyLoadedFromPath(identity, loadDirectoryOpt); + assembly = TryGetAssemblyLoadedFromPath(identity, loadDirectory); if (assembly != null) { return assembly; } // Has an assembly with the same name and version been loaded (possibly from a different directory)? - List loadedInfos; - if (_loadedAssembliesBySimpleName.TryGetValue(identity.Name, out loadedInfos)) + if (_loadedAssembliesBySimpleName.TryGetValue(identity.Name, out var loadedInfos)) { // Desktop FX: A weak-named assembly conflicts with another weak-named assembly of the same simple name, // unless we find an assembly whose identity matches exactly and whose content is exactly the same. @@ -298,14 +280,13 @@ internal Assembly ResolveAssembly(AssemblyIdentity identity, string loadDirector } } - string assemblyFilePathOpt = FindExistingAssemblyFile(identity.Name, loadDirectoryOpt); - if (assemblyFilePathOpt != null) + var assemblyFilePath = FindExistingAssemblyFile(identity.Name, loadDirectory); + if (assemblyFilePath != null) { // TODO: Stop using reflection once ModuleVersionId property once is available in Core contract. - if (!loadedAssemblyWithEqualNameAndVersionOpt.IsDefault) + if (loadedAssemblyWithEqualNameAndVersionOpt.Assembly != null) { - Guid mvid; - if (TryReadMvid(assemblyFilePathOpt, out mvid) && + if (TryReadMvid(assemblyFilePath, out var mvid) && loadedAssemblyWithEqualNameAndVersionOpt.Assembly.ManifestModule.ModuleVersionId == mvid) { return loadedAssemblyWithEqualNameAndVersionOpt.Assembly; @@ -316,8 +297,8 @@ internal Assembly ResolveAssembly(AssemblyIdentity identity, string loadDirector string.Format(null, ScriptingResources.AssemblyAlreadyLoaded, identity.Name, identity.Version, - loadedAssemblyWithEqualNameAndVersionOpt.LocationOpt, - assemblyFilePathOpt) + loadedAssemblyWithEqualNameAndVersionOpt.Location, + assemblyFilePath) ); } @@ -328,12 +309,12 @@ internal Assembly ResolveAssembly(AssemblyIdentity identity, string loadDirector throw new InteractiveAssemblyLoaderException( string.Format(null, ScriptingResources.AssemblyAlreadyLoadedNotSigned, identity.Name, - conflictingLoadedAssemblyOpt.LocationOpt, - assemblyFilePathOpt) + conflictingLoadedAssemblyOpt.Location, + assemblyFilePath) ); } - assembly = ShadowCopyAndLoadDependency(assemblyFilePathOpt).Assembly; + assembly = ShadowCopyAndLoadDependency(assemblyFilePath).Assembly; if (assembly != null) { return assembly; @@ -344,7 +325,7 @@ internal Assembly ResolveAssembly(AssemblyIdentity identity, string loadDirector return GetOrLoadKnownAssembly(identity); } - private static string FindExistingAssemblyFile(string simpleName, string directory) + private static string? FindExistingAssemblyFile(string simpleName, string directory) { string pathWithoutExtension = Path.Combine(directory, simpleName); foreach (var extension in RuntimeMetadataReferenceResolver.AssemblyExtensions) @@ -359,7 +340,7 @@ private static string FindExistingAssemblyFile(string simpleName, string directo return null; } - private Assembly TryGetAssemblyLoadedFromPath(AssemblyIdentity identity, string directory) + private Assembly? TryGetAssemblyLoadedFromPath(AssemblyIdentity identity, string directory) { string pathWithoutExtension = Path.Combine(directory, identity.Name); @@ -397,17 +378,16 @@ private static bool TryReadMvid(string filePath, out Guid mvid) } } - private Assembly GetOrLoadKnownAssembly(AssemblyIdentity identity) + private Assembly? GetOrLoadKnownAssembly(AssemblyIdentity identity) { - Assembly assembly = null; - string assemblyFileToLoad = null; + Assembly? assembly = null; + string? assemblyFileToLoad = null; // Try to find the assembly among assemblies that we loaded, comparing its identity with the requested one. lock (_referencesLock) { // already loaded assemblies: - List infos; - if (_loadedAssembliesBySimpleName.TryGetValue(identity.Name, out infos)) + if (_loadedAssembliesBySimpleName.TryGetValue(identity.Name, out var infos)) { assembly = FindHighestVersionOrFirstMatchingIdentity(identity, infos); if (assembly != null) @@ -417,8 +397,7 @@ private Assembly GetOrLoadKnownAssembly(AssemblyIdentity identity) } // names: - List sameSimpleNameIdentities; - if (_dependenciesWithLocationBySimpleName.TryGetValue(identity.Name, out sameSimpleNameIdentities)) + if (_dependenciesWithLocationBySimpleName.TryGetValue(identity.Name, out var sameSimpleNameIdentities)) { var identityAndLocation = FindHighestVersionOrFirstMatchingIdentity(identity, sameSimpleNameIdentities); if (identityAndLocation.Identity != null) @@ -446,7 +425,7 @@ private AssemblyAndLocation ShadowCopyAndLoadDependency(string originalPath) AssemblyAndLocation assemblyAndLocation = Load(originalPath); if (assemblyAndLocation.IsDefault) { - return default(AssemblyAndLocation); + return default; } lock (_referencesLock) @@ -454,8 +433,7 @@ private AssemblyAndLocation ShadowCopyAndLoadDependency(string originalPath) // Always remember the path. The assembly might have been loaded from another path or not loaded yet. _assembliesLoadedFromLocationByFullPath[originalPath] = assemblyAndLocation; - LoadedAssembly loadedAssembly; - if (_assembliesLoadedFromLocation.TryGetValue(assemblyAndLocation.Assembly, out loadedAssembly)) + if (_assembliesLoadedFromLocation.TryGetValue(assemblyAndLocation.Assembly, out var loadedAssembly)) { return assemblyAndLocation; } @@ -470,10 +448,10 @@ private AssemblyAndLocation ShadowCopyAndLoadDependency(string originalPath) return assemblyAndLocation; } - private static Assembly FindHighestVersionOrFirstMatchingIdentity(AssemblyIdentity identity, IEnumerable infos) + private static Assembly? FindHighestVersionOrFirstMatchingIdentity(AssemblyIdentity identity, IEnumerable infos) { - Assembly candidate = null; - Version candidateVersion = null; + Assembly? candidate = null; + Version? candidateVersion = null; foreach (var info in infos) { if (DesktopAssemblyIdentityComparer.Default.ReferenceMatchesDefinition(identity, info.Identity)) diff --git a/src/Scripting/Core/Microsoft.CodeAnalysis.Scripting.csproj b/src/Scripting/Core/Microsoft.CodeAnalysis.Scripting.csproj index ca855a03458bf..127c5b51a41dc 100644 --- a/src/Scripting/Core/Microsoft.CodeAnalysis.Scripting.csproj +++ b/src/Scripting/Core/Microsoft.CodeAnalysis.Scripting.csproj @@ -20,10 +20,6 @@ - - - - Hosting\Resolvers\RelativePathResolver.cs diff --git a/src/Scripting/Core/Utilities/PdbHelpers.cs b/src/Scripting/Core/Utilities/PdbHelpers.cs index fe8f6747b2a88..a4d1b5ea0e52b 100644 --- a/src/Scripting/Core/Utilities/PdbHelpers.cs +++ b/src/Scripting/Core/Utilities/PdbHelpers.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -#nullable disable - using System; using Microsoft.CodeAnalysis.Emit; @@ -13,14 +11,19 @@ internal static class PdbHelpers { public static DebugInformationFormat GetPlatformSpecificDebugInformationFormat() { - // for CoreCLR & Mono, use PortablePdb - if (CoreClrShim.AssemblyLoadContext.Type != null || Type.GetType("Mono.Runtime") != null) +#if NET + // Use PortablePdb for .NET + return DebugInformationFormat.PortablePdb; +#else + // Use PortablePdb for Mono + if (Type.GetType("Mono.Runtime") != null) { return DebugInformationFormat.PortablePdb; } // otherwise standard PDB return DebugInformationFormat.Pdb; +#endif } } }