ILMerge generated assembly doesn't run, although log output reports no errors - why is this?

前端 未结 5 1070
一向
一向 2021-02-03 11:41

I\'m testing out ILMerge for a new project, and although the .exe file seems to be created correctly, it won\'t run.

I have installed ILMerge via the .msi installer (fou

5条回答
  •  爱一瞬间的悲伤
    2021-02-03 12:32

    A fully self-contained executable can be created in Visual Studio 2010 easily, and this method does not require ILMerge at all.

    Steps:

    • Create an empty executable project
    • Add your DLLs (and your EXE file) as resources. (Right click on project's name in Solution Explorer, Properties, Resources, Add Resource)

    After this Visual Studio will auto-create a new class called Resources, which you have to use in your new project.

    • Add the two methods below to Program.cs to make the final application work.

    Then you have to compile your program with Visual Studio and that's it.

    static void Main(string[] args)
    {
        // Load your assembly with the entry point from resources:
        Assembly start = Assembly.Load((byte[]) Properties.Resources.NameOfDllOrExeInYourResources);
        Type t = start.GetType("Foo.Bar.Program");
    
        // Install the resolver event
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    
        // Find Program.Main in the loaded assembly
        MethodInfo m = t.GetMethod("Main", BindingFlags.Public | BindingFlags.Static);
    
        // Launch the application
        m.Invoke(null, new object[] { args });
    }
    
    static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        string assemblyName = new AssemblyName(args.Name).Name.Replace('.', '_');
    
        // Locate and load the contents of the resource 
        byte[] assemblyBytes = (byte[]) Properties.Resources.ResourceManager.GetObject(assemblyName, Properties.Resources.Culture);
    
        // Return the loaded assembly
        return Assembly.Load(assemblyBytes);
    }
    

提交回复
热议问题