-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssemblyMetadataLoader.cs
127 lines (109 loc) · 4.41 KB
/
AssemblyMetadataLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Globalization;
using System.Reflection;
namespace BindingRedirectR
{
internal static class AssemblyMetadataLoader
{
public static AssemblyMetadata ReflectionOnlyLoadFrom(string path)
{
using (var tempAppDomain = new TempAppDomain())
{
var assemblyMetadata = tempAppDomain.ReflectionOnlyLoadFrom(path);
return assemblyMetadata;
}
}
public static AssemblyMetadata ReflectionOnlyLoad(AssemblyName name)
{
using (var tempAppDomain = new TempAppDomain())
{
var assemblyMetadata = tempAppDomain.ReflectionOnlyLoad(name.FullName);
return assemblyMetadata;
}
}
public static AssemblyMetadata LoadFrom(string path)
{
using (var tempAppDomain = new TempAppDomain())
{
var assemblyMetadata = tempAppDomain.LoadFrom(path);
return assemblyMetadata;
}
}
public static AssemblyMetadata Load(AssemblyName name)
{
using (var tempAppDomain = new TempAppDomain())
{
var assemblyMetadata = tempAppDomain.Load(name.FullName);
return assemblyMetadata;
}
}
private class TempAppDomain : IDisposable
{
private readonly AppDomain _appDomain;
private readonly AssemblyLoaderProxy _loaderProxy;
public TempAppDomain()
{
var settings = new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
};
_appDomain = AppDomain.CreateDomain(Guid.NewGuid().ToString(), securityInfo: null, settings);
var handle = Activator.CreateInstance
(
_appDomain,
typeof(AssemblyLoaderProxy).Assembly.FullName,
// ReSharper disable once AssignNullToNotNullAttribute
typeof(AssemblyLoaderProxy).FullName,
ignoreCase: false,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance,
binder: null,
args: null,
CultureInfo.CurrentCulture,
activationAttributes: new object[0]
);
_loaderProxy = (AssemblyLoaderProxy)handle.Unwrap();
}
// ReSharper disable once MemberHidesStaticFromOuterClass
public AssemblyMetadata ReflectionOnlyLoadFrom(string path)
=> _loaderProxy.ReflectionOnlyLoadFrom(path);
public AssemblyMetadata ReflectionOnlyLoad(string assemblyName)
=> _loaderProxy.ReflectionOnlyLoad(assemblyName);
public AssemblyMetadata LoadFrom(string path)
=> _loaderProxy.LoadFrom(path);
public AssemblyMetadata Load(string assemblyName)
=> _loaderProxy.Load(assemblyName);
public void Dispose()
{
AppDomain.Unload(_appDomain);
}
}
private class AssemblyLoaderProxy : MarshalByRefObject
{
// ReSharper disable once MemberHidesStaticFromOuterClass
public AssemblyMetadata ReflectionOnlyLoadFrom(string path)
{
var assembly = Assembly.ReflectionOnlyLoadFrom(path);
var assemblyMetadata = new AssemblyMetadata(assembly);
return assemblyMetadata;
}
public AssemblyMetadata ReflectionOnlyLoad(string assemblyName)
{
var assembly = Assembly.ReflectionOnlyLoad(assemblyName);
var assemblyMetadata = new AssemblyMetadata(assembly);
return assemblyMetadata;
}
public AssemblyMetadata LoadFrom(string path)
{
var assembly = Assembly.LoadFrom(path);
var assemblyMetadata = new AssemblyMetadata(assembly);
return assemblyMetadata;
}
public AssemblyMetadata Load(string assemblyName)
{
var assembly = Assembly.Load(assemblyName);
var assemblyMetadata = new AssemblyMetadata(assembly);
return assemblyMetadata;
}
}
}
}