···11-using System;
22-using System.Reflection;
33-using System.IO;
44-55-namespace @NAMESPACE@
66-{
77- class @MAINCLASSNAME@Wrapper
88- {
99- private String[] AssemblySearchPaths = { @ASSEMBLYSEARCHPATHS@ };
1010-1111- public @MAINCLASSNAME@Wrapper()
1212- {
1313- AppDomain currentDomain = AppDomain.CurrentDomain;
1414- currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
1515- }
1616-1717- static void Main(string[] args)
1818- {
1919- // Initialise the wrapper so that the missing library assemblies are loaded
2020- new @MAINCLASSNAME@Wrapper();
2121-2222- // Call the original main method
2323- @MAINCLASSNAME@.Main2(args);
2424- }
2525-2626- private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
2727- {
2828- //This handler is called only when the common language runtime tries to bind to the assembly and fails.
2929-3030- //Retrieve the list of referenced assemblies in an array of AssemblyName.
3131- Assembly MyAssembly, executingAssemblies;
3232- string assemblyPath = "";
3333-3434- executingAssemblies = Assembly.GetExecutingAssembly();
3535- AssemblyName[] referencedAssemblies = executingAssemblies.GetReferencedAssemblies();
3636-3737- //Loop through the array of referenced assembly names.
3838- foreach (AssemblyName assemblyName in referencedAssemblies)
3939- {
4040- //Check for the assembly names that have raised the "AssemblyResolve" event.
4141- if (assemblyName.FullName.Substring(0, assemblyName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
4242- {
4343- //Retrieve the name of the assembly from where it has to be loaded.
4444- String dllName = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
4545-4646- //Search for the right path of the library assembly
4747- foreach (String currentAssemblyPath in AssemblySearchPaths)
4848- {
4949- assemblyPath = currentAssemblyPath + "/" + dllName;
5050- if (File.Exists(assemblyPath))
5151- break;
5252- }
5353- }
5454- }
5555-5656- //Load the assembly from the specified path.
5757- MyAssembly = Assembly.LoadFrom(assemblyPath);
5858-5959- //Return the loaded assembly.
6060- return MyAssembly;
6161- }
6262-6363- }
6464-}
···11+using System;
22+using System.Reflection;
33+using System.IO;
44+55+namespace @NAMESPACE@Wrapper
66+{
77+ class @MAINCLASSNAME@Wrapper
88+ {
99+ private String[] AssemblySearchPaths = { @ASSEMBLYSEARCHPATH@ };
1010+1111+ private String ExePath = @"@EXEPATH@";
1212+1313+ private String MainClassName = "@NAMESPACE@.@MAINCLASSNAME@";
1414+1515+ private Assembly exeAssembly;
1616+1717+ public @MAINCLASSNAME@Wrapper(string[] args)
1818+ {
1919+ // Attach the resolve event handler to the AppDomain so that missing library assemblies will be searched
2020+ AppDomain currentDomain = AppDomain.CurrentDomain;
2121+ currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
2222+2323+ // Dynamically load the executable assembly
2424+ exeAssembly = Assembly.LoadFrom(ExePath);
2525+2626+ // Lookup the main class
2727+ Type mainClass = exeAssembly.GetType(MainClassName);
2828+2929+ // Lookup the main method
3030+ MethodInfo mainMethod = mainClass.GetMethod("Main");
3131+3232+ // Invoke the main method
3333+ mainMethod.Invoke(this, new Object[] {args});
3434+ }
3535+3636+ static void Main(string[] args)
3737+ {
3838+ new @MAINCLASSNAME@Wrapper(args);
3939+ }
4040+4141+ private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
4242+ {
4343+ //This handler is called only when the common language runtime tries to bind to the assembly and fails.
4444+4545+ //Retrieve the list of referenced assemblies in an array of AssemblyName.
4646+ Assembly MyAssembly;
4747+ string assemblyPath = "";
4848+4949+ AssemblyName[] referencedAssemblies = exeAssembly.GetReferencedAssemblies();
5050+5151+ //Loop through the array of referenced assembly names.
5252+ foreach (AssemblyName assemblyName in referencedAssemblies)
5353+ {
5454+ //Check for the assembly names that have raised the "AssemblyResolve" event.
5555+ if (assemblyName.FullName.Substring(0, assemblyName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(",")))
5656+ {
5757+ //Retrieve the name of the assembly from where it has to be loaded.
5858+ String dllName = args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll";
5959+6060+ //Search for the right path of the library assembly
6161+ foreach (String currentAssemblyPath in AssemblySearchPaths)
6262+ {
6363+ assemblyPath = currentAssemblyPath + "/" + dllName;
6464+ if (File.Exists(assemblyPath))
6565+ break;
6666+ }
6767+ }
6868+ }
6969+7070+ //Load the assembly from the specified path.
7171+ MyAssembly = Assembly.LoadFrom(assemblyPath);
7272+7373+ //Return the loaded assembly.
7474+ return MyAssembly;
7575+ }
7676+7777+ }
7878+}