-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryDetailFactory.cs
More file actions
170 lines (145 loc) · 6.41 KB
/
BinaryDetailFactory.cs
File metadata and controls
170 lines (145 loc) · 6.41 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Security.Policy;
namespace BinaryDetailer
{
public class BinaryDetailFactory
{
public BinaryDetail CreateBinaryDetail(FileInfo binaryFileInfo)
{
if (string.IsNullOrEmpty(binaryFileInfo.Directory.FullName))
throw new InvalidOperationException(
"Directory can't be null or empty.");
if (!Directory.Exists(binaryFileInfo.Directory.FullName))
throw new InvalidOperationException(
string.Format(CultureInfo.CurrentCulture,
"Directory not found {0}",
binaryFileInfo.Directory.FullName));
var childDomain = BuildChildDomain(
AppDomain.CurrentDomain);
try
{
var bd = new BinaryDetail(binaryFileInfo);
var loaderType = typeof(BinaryDetailPopulator);
if (loaderType.Assembly != null)
{
var loader =
(BinaryDetailPopulator) childDomain.CreateInstanceFrom(
loaderType.Assembly.Location,
loaderType.FullName).Unwrap();
try
{
loader.LoadAssembly(
binaryFileInfo.FullName);
bd = loader.PopulateBinaryDetail(bd);
}
catch (Exception e)
{
// this is the error thrown I think where we try to load a native dll using ReflectionOnlyLoadFrom, so ignore that specific message
if (!e.Message.Contains("The module was expected to contain an assembly manifest."))
{
bd.Error = e.Message;
}
}
}
return bd;
}
finally
{
AppDomain.Unload(childDomain);
}
}
/// <summary>
/// Creates a new AppDomain based on the parent AppDomains
/// Evidence and AppDomainSetup
/// </summary>
/// <param name="parentDomain">The parent AppDomain</param>
/// <returns>A newly created AppDomain</returns>
private AppDomain BuildChildDomain(AppDomain parentDomain)
{
var evidence = new Evidence(parentDomain.Evidence);
var setup = parentDomain.SetupInformation;
return AppDomain.CreateDomain("DiscoveryRegion",
evidence, setup);
}
private class BinaryDetailPopulator : MarshalByRefObject
{
[SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic")]
internal BinaryDetail PopulateBinaryDetail(BinaryDetail binaryDetail)
{
var directory = new DirectoryInfo(binaryDetail.FileInfo.DirectoryName);
ResolveEventHandler resolveEventHandler =
(s, e) =>
{
return OnReflectionOnlyResolve(
e, directory);
};
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve
+= resolveEventHandler;
var reflectionOnlyAssembly =
AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies().First();
binaryDetail.ImageRuntimeVersion = reflectionOnlyAssembly.ImageRuntimeVersion;
binaryDetail.AssemblyVersion = reflectionOnlyAssembly.GetName().Version.ToString();
PortableExecutableKinds peKind;
ImageFileMachine machine;
reflectionOnlyAssembly.ManifestModule.GetPEKind(out peKind, out machine);
binaryDetail.PEKind = peKind;
binaryDetail.ImageFileMachine = machine;
var pe = reflectionOnlyAssembly.GetName().ProcessorArchitecture;
binaryDetail.ProcessorArchitecture = pe;
binaryDetail.TargetFrameworkAttribute = GetCustomAttributeValue(reflectionOnlyAssembly, typeof(TargetFrameworkAttribute));
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve
-= resolveEventHandler;
return binaryDetail;
}
private string GetCustomAttributeValue(Assembly assembly, Type type)
{
var attributesList = assembly.CustomAttributes
.Where(a => a.AttributeType == type).ToList();
if (attributesList.Count == 1)
return attributesList[0].ConstructorArguments[0].ToString().Replace("\"", "");
return string.Empty;
}
private Assembly OnReflectionOnlyResolve(
ResolveEventArgs args, DirectoryInfo directory)
{
var loadedAssembly =
AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies()
.FirstOrDefault(
asm => string.Equals(asm.FullName, args.Name,
StringComparison.OrdinalIgnoreCase));
if (loadedAssembly != null) return loadedAssembly;
var assemblyName =
new AssemblyName(args.Name);
var dependentAssemblyFilename =
Path.Combine(directory.FullName,
assemblyName.Name + ".dll");
if (File.Exists(dependentAssemblyFilename))
return Assembly.ReflectionOnlyLoadFrom(
dependentAssemblyFilename);
return Assembly.ReflectionOnlyLoad(args.Name);
}
[SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic")]
internal void LoadAssembly(string assemblyPath)
{
try
{
Assembly.ReflectionOnlyLoadFrom(assemblyPath);
}
catch (FileNotFoundException)
{
/* Continue loading assemblies even if an assembly
* can not be loaded in the new AppDomain. */
}
}
}
}
}