blob: 4ea0d6ee6fab96a66462daee0c9057d4d2c5c42d (
plain) (
blame)
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;
using System.Reflection;
using System.IO;
namespace @NAMESPACE@Wrapper
{
class @MAINCLASSNAME@Wrapper
{
private String[] AssemblySearchPaths = { @ASSEMBLYSEARCHPATH@ };
private String ExePath = @"@EXEPATH@";
private String MainClassName = "@NAMESPACE@.@MAINCLASSNAME@";
private Assembly exeAssembly;
public @MAINCLASSNAME@Wrapper(string[] args)
{
// Attach the resolve event handler to the AppDomain so that missing library assemblies will be searched
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
// Dynamically load the executable assembly
exeAssembly = Assembly.LoadFrom(ExePath);
// Lookup the main class
Type mainClass = exeAssembly.GetType(MainClassName);
// Lookup the main method
MethodInfo mainMethod = mainClass.GetMethod("Main");
// Invoke the main method
mainMethod.Invoke(this, new Object[] {args});
}
static void Main(string[] args)
{
new @MAINCLASSNAME@Wrapper(args);
}
private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
//This handler is called only when the common language runtime tries to bind to the assembly and fails.
//Retrieve the list of referenced assemblies in an array of AssemblyName.
Assembly MyAssembly;
string assemblyPath = "";
AssemblyName[] referencedAssemblies = exeAssembly.GetReferencedAssemblies();
//Loop through the array of referenced assembly names.
foreach (AssemblyName assemblyName in referencedAssemblies)
{
//Check for the assembly names that have raised the "AssemblyResolve" event.
if (assemblyName.FullName.Substring(0, assemblyName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
{
//Retrieve the name of the assembly from where it has to be loaded.
String dllName = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
//Search for the right path of the library assembly
foreach (String currentAssemblyPath in AssemblySearchPaths)
{
assemblyPath = currentAssemblyPath + "/" + dllName;
if (File.Exists(assemblyPath))
break;
}
}
}
//Load the assembly from the specified path.
MyAssembly = Assembly.LoadFrom(assemblyPath);
//Return the loaded assembly.
return MyAssembly;
}
}
}
|