-
Notifications
You must be signed in to change notification settings - Fork 2
/
AssemblyLoader.cs
77 lines (69 loc) · 2.31 KB
/
AssemblyLoader.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace UsageRateTool
{
class AssemblyLoader
{
static AssemblyLoader()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += MyResolveEventHandler;
}
public static Assembly GetAssemblyByName(AssemblyName name)
{
string cwd = Directory.GetCurrentDirectory();
return FindDll(cwd, name.FullName);
}
public static Assembly GetAssembly(string path)
{
string cwd = Directory.GetCurrentDirectory();
string source = Path.Combine(cwd, path);
try
{
return Assembly.LoadFrom(source);
}
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
if (exFileNotFound != null)
{
if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
Console.WriteLine(errorMessage);
}
return null;
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
var cwd = Directory.GetCurrentDirectory();
return FindDll(cwd, args.Name);
}
static Assembly FindDll(string dir, string fullName)
{
var files = Directory.GetFiles(dir, "*.dll", SearchOption.AllDirectories);
foreach (var file in files)
{
var asm = Assembly.LoadFile(file);
if (fullName == asm.FullName)
{
return asm;
}
}
return null;
}
}
}