diff options
Diffstat (limited to 'src/mscorlib/src/System/Runtime/Loader')
-rw-r--r-- | src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs | 92 |
1 files changed, 70 insertions, 22 deletions
diff --git a/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs b/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs index 75529868bd..e158a5aa8a 100644 --- a/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs +++ b/src/mscorlib/src/System/Runtime/Loader/AssemblyLoadContext.cs @@ -17,7 +17,6 @@ using System.Threading; namespace System.Runtime.Loader { - [System.Security.SecuritySafeCritical] public abstract class AssemblyLoadContext { [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] @@ -62,7 +61,6 @@ namespace System.Runtime.Loader InitializeLoadContext(fRepresentsTPALoadContext); } - [System.Security.SecuritySafeCritical] void InitializeLoadContext(bool fRepresentsTPALoadContext) { // Initialize the VM side of AssemblyLoadContext if not already done. @@ -83,6 +81,17 @@ namespace System.Runtime.Loader [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] [SuppressUnmanagedCodeSecurity] private static extern void LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, string ilPath, string niPath, ObjectHandleOnStack retAssembly); + + [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)] + [SuppressUnmanagedCodeSecurity] + private static extern void GetLoadedAssembliesInternal(ObjectHandleOnStack assemblies); + + public static Assembly[] GetLoadedAssemblies() + { + Assembly[] assemblies = null; + GetLoadedAssembliesInternal(JitHelpers.GetObjectHandleOnStack(ref assemblies)); + return assemblies; + } // These are helpers that can be used by AssemblyLoadContext derivations. // They are used to load assemblies in DefaultContext. @@ -90,12 +99,12 @@ namespace System.Runtime.Loader { if (assemblyPath == null) { - throw new ArgumentNullException("assemblyPath"); + throw new ArgumentNullException(nameof(assemblyPath)); } - if (Path.IsRelative(assemblyPath)) + if (PathInternal.IsPartiallyQualified(assemblyPath)) { - throw new ArgumentException( Environment.GetResourceString("Argument_AbsolutePathRequired"), "assemblyPath"); + throw new ArgumentException( Environment.GetResourceString("Argument_AbsolutePathRequired"), nameof(assemblyPath)); } RuntimeAssembly loadedAssembly = null; @@ -107,17 +116,17 @@ namespace System.Runtime.Loader { if (nativeImagePath == null) { - throw new ArgumentNullException("nativeImagePath"); + throw new ArgumentNullException(nameof(nativeImagePath)); } - if (Path.IsRelative(nativeImagePath)) + if (PathInternal.IsPartiallyQualified(nativeImagePath)) { - throw new ArgumentException( Environment.GetResourceString("Argument_AbsolutePathRequired"), "nativeImagePath"); + throw new ArgumentException( Environment.GetResourceString("Argument_AbsolutePathRequired"), nameof(nativeImagePath)); } - if (assemblyPath != null && Path.IsRelative(assemblyPath)) + if (assemblyPath != null && PathInternal.IsPartiallyQualified(assemblyPath)) { - throw new ArgumentException(Environment.GetResourceString("Argument_AbsolutePathRequired"), "assemblyPath"); + throw new ArgumentException(Environment.GetResourceString("Argument_AbsolutePathRequired"), nameof(assemblyPath)); } // Basic validation has succeeded - lets try to load the NI image. @@ -136,7 +145,7 @@ namespace System.Runtime.Loader { if (assembly == null) { - throw new ArgumentNullException("assembly"); + throw new ArgumentNullException(nameof(assembly)); } int iAssemblyStreamLength = (int)assembly.Length; @@ -291,15 +300,15 @@ namespace System.Runtime.Loader { if (unmanagedDllPath == null) { - throw new ArgumentNullException("unmanagedDllPath"); + throw new ArgumentNullException(nameof(unmanagedDllPath)); } if (unmanagedDllPath.Length == 0) { - throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"), "unmanagedDllPath"); + throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"), nameof(unmanagedDllPath)); } - if (Path.IsRelative(unmanagedDllPath)) + if (PathInternal.IsPartiallyQualified(unmanagedDllPath)) { - throw new ArgumentException(Environment.GetResourceString("Argument_AbsolutePathRequired"), "unmanagedDllPath"); + throw new ArgumentException(Environment.GetResourceString("Argument_AbsolutePathRequired"), nameof(unmanagedDllPath)); } return InternalLoadUnmanagedDllFromPath(unmanagedDllPath); @@ -355,7 +364,7 @@ namespace System.Runtime.Loader { if (context == null) { - throw new ArgumentNullException("context"); + throw new ArgumentNullException(nameof(context)); } // Try to override the default assembly load context @@ -378,10 +387,10 @@ namespace System.Runtime.Loader { if (assemblyPath == null) { - throw new ArgumentNullException("assemblyPath"); + throw new ArgumentNullException(nameof(assemblyPath)); } - - String fullPath = Path.GetFullPathInternal(assemblyPath); + + string fullPath = Path.GetFullPath(assemblyPath); return nGetFileInformation(fullPath); } @@ -394,7 +403,7 @@ namespace System.Runtime.Loader { if (assembly == null) { - throw new ArgumentNullException("assembly"); + throw new ArgumentNullException(nameof(assembly)); } AssemblyLoadContext loadContextForAssembly = null; @@ -459,16 +468,43 @@ namespace System.Runtime.Loader // Synchronization primitive for controlling initialization of Default load context private static readonly object s_initLock = new Object(); + + // Occurs when an Assembly is loaded + public static event AssemblyLoadEventHandler AssemblyLoad + { + add { AppDomain.CurrentDomain.AssemblyLoad += value; } + remove { AppDomain.CurrentDomain.AssemblyLoad -= value; } + } + + // Occurs when resolution of type fails + public static event ResolveEventHandler TypeResolve + { + add { AppDomain.CurrentDomain.TypeResolve += value; } + remove { AppDomain.CurrentDomain.TypeResolve -= value; } + } + + // Occurs when resolution of resource fails + public static event ResolveEventHandler ResourceResolve + { + add { AppDomain.CurrentDomain.ResourceResolve += value; } + remove { AppDomain.CurrentDomain.ResourceResolve -= value; } + } + + // Occurs when resolution of assembly fails + // This event is fired after resolve events of AssemblyLoadContext fails + public static event ResolveEventHandler AssemblyResolve + { + add { AppDomain.CurrentDomain.AssemblyResolve += value; } + remove { AppDomain.CurrentDomain.AssemblyResolve -= value; } + } } - [System.Security.SecuritySafeCritical] class AppPathAssemblyLoadContext : AssemblyLoadContext { internal AppPathAssemblyLoadContext() : base(true) { } - [System.Security.SecuritySafeCritical] protected override Assembly Load(AssemblyName assemblyName) { // We were loading an assembly into TPA ALC that was not found on TPA list. As a result we are here. @@ -476,6 +512,18 @@ namespace System.Runtime.Loader return null; } } + + internal class IndividualAssemblyLoadContext : AssemblyLoadContext + { + internal IndividualAssemblyLoadContext() : base(false) + { + } + + protected override Assembly Load(AssemblyName assemblyName) + { + return null; + } + } } #endif // FEATURE_HOST_ASSEMBLY_RESOLVER |