AssemblyHelper
AssemblyHelper provides static methods for working
with assemblies.
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
namespace NUnit.Framework.Internal
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class AssemblyHelper
{
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class ReflectionAssemblyLoader
{
[System.Runtime.CompilerServices.Nullable(2)]
private static ReflectionAssemblyLoader _instance;
private static bool _isInitialized;
private readonly Func<string, Assembly> _loadFromAssemblyPath;
public Assembly LoadFromAssemblyPath(string assemblyPath)
{
return _loadFromAssemblyPath(assemblyPath);
}
private ReflectionAssemblyLoader(Func<string, Assembly> loadFromAssemblyPath)
{
_loadFromAssemblyPath = loadFromAssemblyPath;
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static ReflectionAssemblyLoader TryGet()
{
if (_isInitialized)
return _instance;
_instance = TryInitialize();
_isInitialized = true;
return _instance;
}
[System.Runtime.CompilerServices.NullableContext(2)]
private static ReflectionAssemblyLoader TryInitialize()
{
Type type = Type.GetType("System.Runtime.Loader.AssemblyLoadContext", false);
if ((object)type == null)
return null;
object value = type.GetRuntimeProperty("Default").GetValue(null);
Func<string, Assembly> loadFromAssemblyPath = (Func<string, Assembly>)type.GetRuntimeMethod("LoadFromAssemblyPath", new Type[1] {
typeof(string)
}).CreateDelegate(typeof(Func<string, Assembly>), value);
type.GetRuntimeEvent("Resolving").AddEventHandler(value, (Func<AssemblyLoadContext, AssemblyName, Assembly>)delegate(AssemblyLoadContext context, AssemblyName assemblyName) {
string text = Path.Combine(AppContext.BaseDirectory, assemblyName.Name + ".dll");
if (File.Exists(text))
return loadFromAssemblyPath(text);
string text2 = Path.Combine(AppContext.BaseDirectory, assemblyName.Name + ".exe");
if (File.Exists(text2))
return loadFromAssemblyPath(text2);
return null;
});
return new ReflectionAssemblyLoader(loadFromAssemblyPath);
}
}
private static readonly string UriSchemeFile = Uri.UriSchemeFile;
private static readonly string SchemeDelimiter = Uri.SchemeDelimiter;
public static string GetAssemblyPath(Assembly assembly)
{
return assembly.Location;
}
public static string GetDirectoryName(Assembly assembly)
{
return Path.GetDirectoryName(GetAssemblyPath(assembly));
}
public static AssemblyName GetAssemblyName(Assembly assembly)
{
return assembly.GetName();
}
public static Assembly Load(string name)
{
string extension = Path.GetExtension(name);
if (extension.Equals(".dll", StringComparison.OrdinalIgnoreCase) || extension.Equals(".exe", StringComparison.OrdinalIgnoreCase)) {
Assembly assembly = ReflectionAssemblyLoader.TryGet()?.LoadFromAssemblyPath(Path.GetFullPath(name));
if ((object)assembly != null)
return assembly;
name = Path.GetFileNameWithoutExtension(name);
}
return Assembly.Load(new AssemblyName {
Name = name
});
}
private static bool IsFileUri(string uri)
{
return uri.StartsWith(UriSchemeFile, StringComparison.OrdinalIgnoreCase);
}
public static string GetAssemblyPathFromCodeBase(string codeBase)
{
int num = UriSchemeFile.Length + SchemeDelimiter.Length;
if (codeBase[num] == '/') {
if (codeBase[num + 2] == ':')
num++;
} else if (codeBase[num + 1] != ':') {
num -= 2;
}
return codeBase.Substring(num);
}
}
}