-
Notifications
You must be signed in to change notification settings - Fork 1
/
AssemblyResolver.cs
78 lines (65 loc) · 2.37 KB
/
AssemblyResolver.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
using System.Collections.Generic;
using System.IO;
using Mono.Cecil;
namespace Blur
{
internal class AssemblyResolver : IAssemblyResolver
{
private readonly Dictionary<string, AssemblyDefinition> assembliesResolved = new Dictionary<string, AssemblyDefinition>();
/// <inheritdoc/>
public AssemblyDefinition Resolve(string fullName)
{
return Resolve(fullName, new ReaderParameters());
}
/// <inheritdoc/>
public AssemblyDefinition Resolve(AssemblyNameReference name)
{
return Resolve(name, new ReaderParameters());
}
/// <inheritdoc/>
public AssemblyDefinition Resolve(string fullName, ReaderParameters parameters)
{
// Fix bad formatting of the fullname:
// Sometimes the "Version=, Culture=, PublicKeyToken=" part of the assembly
// appears more than once in the full name; keep only the first part.
for (int i = 0, commaCount = 0; i < fullName.Length; i++)
{
if (fullName[i] != ',')
continue;
if (++commaCount != 4)
continue;
fullName = fullName.Substring(0, i);
break;
}
if (assembliesResolved.TryGetValue(fullName, out AssemblyDefinition assembly))
return assembly;
Stream targetStream = Processor.GetAssemblyStream(fullName);
if (targetStream == null)
return null;
try
{
assembly = AssemblyDefinition.ReadAssembly(targetStream, new ReaderParameters
{
InMemory = true,
ReadWrite = false,
ReadingMode = ReadingMode.Immediate
});
this.assembliesResolved.Add(fullName, assembly);
return assembly;
}
catch
{
return null;
}
}
/// <inheritdoc/>
public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
{
return Resolve(name.FullName, parameters);
}
public void Dispose()
{
// Nothing to dispose.
}
}
}