AssemblyHelper
AssemblyHelper provides static methods for working
with assemblies.
using System;
using System.IO;
using System.Reflection;
namespace NUnit.Framework.Internal
{
public static class AssemblyHelper
{
private sealed class ReflectionAssemblyLoader
{
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)
{
this.loadFromAssemblyPath = loadFromAssemblyPath;
}
public static ReflectionAssemblyLoader TryGet()
{
if (isInitialized)
return instance;
instance = TryInitialize();
isInitialized = true;
return instance;
}
private static ReflectionAssemblyLoader TryInitialize()
{
Type type = Type.GetType("System.Runtime.Loader.AssemblyLoadContext", false);
if (type == (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<object, AssemblyName, Assembly>)delegate(object 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)
{
string codeBase = assembly.CodeBase;
if (IsFileUri(codeBase))
return GetAssemblyPathFromCodeBase(codeBase);
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 (assembly != (Assembly)null)
return assembly;
name = Path.GetFileNameWithoutExtension(name);
}
return Assembly.Load(new AssemblyName {
Name = name
});
}
private static bool IsFileUri(string uri)
{
return uri.ToLower().StartsWith(UriSchemeFile);
}
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);
}
}
}