···1-using System;
2-using System.Reflection;
3-using System.IO;
4-5-namespace @NAMESPACE@
6-{
7- class @MAINCLASSNAME@Wrapper
8- {
9- private String[] AssemblySearchPaths = { @ASSEMBLYSEARCHPATHS@ };
10-11- public @MAINCLASSNAME@Wrapper()
12- {
13- AppDomain currentDomain = AppDomain.CurrentDomain;
14- currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
15- }
16-17- static void Main(string[] args)
18- {
19- // Initialise the wrapper so that the missing library assemblies are loaded
20- new @MAINCLASSNAME@Wrapper();
21-22- // Call the original main method
23- @MAINCLASSNAME@.Main2(args);
24- }
25-26- private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
27- {
28- //This handler is called only when the common language runtime tries to bind to the assembly and fails.
29-30- //Retrieve the list of referenced assemblies in an array of AssemblyName.
31- Assembly MyAssembly, executingAssemblies;
32- string assemblyPath = "";
33-34- executingAssemblies = Assembly.GetExecutingAssembly();
35- AssemblyName[] referencedAssemblies = executingAssemblies.GetReferencedAssemblies();
36-37- //Loop through the array of referenced assembly names.
38- foreach (AssemblyName assemblyName in referencedAssemblies)
39- {
40- //Check for the assembly names that have raised the "AssemblyResolve" event.
41- if (assemblyName.FullName.Substring(0, assemblyName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
42- {
43- //Retrieve the name of the assembly from where it has to be loaded.
44- String dllName = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
45-46- //Search for the right path of the library assembly
47- foreach (String currentAssemblyPath in AssemblySearchPaths)
48- {
49- assemblyPath = currentAssemblyPath + "/" + dllName;
50- if (File.Exists(assemblyPath))
51- break;
52- }
53- }
54- }
55-56- //Load the assembly from the specified path.
57- MyAssembly = Assembly.LoadFrom(assemblyPath);
58-59- //Return the loaded assembly.
60- return MyAssembly;
61- }
62-63- }
64-}
···1+using System;
2+using System.Reflection;
3+using System.IO;
4+5+namespace @NAMESPACE@Wrapper
6+{
7+ class @MAINCLASSNAME@Wrapper
8+ {
9+ private String[] AssemblySearchPaths = { @ASSEMBLYSEARCHPATH@ };
10+11+ private String ExePath = @"@EXEPATH@";
12+13+ private String MainClassName = "@NAMESPACE@.@MAINCLASSNAME@";
14+15+ private Assembly exeAssembly;
16+17+ public @MAINCLASSNAME@Wrapper(string[] args)
18+ {
19+ // Attach the resolve event handler to the AppDomain so that missing library assemblies will be searched
20+ AppDomain currentDomain = AppDomain.CurrentDomain;
21+ currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
22+23+ // Dynamically load the executable assembly
24+ exeAssembly = Assembly.LoadFrom(ExePath);
25+26+ // Lookup the main class
27+ Type mainClass = exeAssembly.GetType(MainClassName);
28+29+ // Lookup the main method
30+ MethodInfo mainMethod = mainClass.GetMethod("Main");
31+32+ // Invoke the main method
33+ mainMethod.Invoke(this, new Object[] {args});
34+ }
35+36+ static void Main(string[] args)
37+ {
38+ new @MAINCLASSNAME@Wrapper(args);
39+ }
40+41+ private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
42+ {
43+ //This handler is called only when the common language runtime tries to bind to the assembly and fails.
44+45+ //Retrieve the list of referenced assemblies in an array of AssemblyName.
46+ Assembly MyAssembly;
47+ string assemblyPath = "";
48+49+ AssemblyName[] referencedAssemblies = exeAssembly.GetReferencedAssemblies();
50+51+ //Loop through the array of referenced assembly names.
52+ foreach (AssemblyName assemblyName in referencedAssemblies)
53+ {
54+ //Check for the assembly names that have raised the "AssemblyResolve" event.
55+ if (assemblyName.FullName.Substring(0, assemblyName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
56+ {
57+ //Retrieve the name of the assembly from where it has to be loaded.
58+ String dllName = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
59+60+ //Search for the right path of the library assembly
61+ foreach (String currentAssemblyPath in AssemblySearchPaths)
62+ {
63+ assemblyPath = currentAssemblyPath + "/" + dllName;
64+ if (File.Exists(assemblyPath))
65+ break;
66+ }
67+ }
68+ }
69+70+ //Load the assembly from the specified path.
71+ MyAssembly = Assembly.LoadFrom(assemblyPath);
72+73+ //Return the loaded assembly.
74+ return MyAssembly;
75+ }
76+77+ }
78+}