ILMerge替代,如何将应用程序的依赖DLL嵌入到EXE文件中? [英] ILMerge alternative, how to embed application’s dependent DLLs inside an EXE file?

查看:270
本文介绍了ILMerge替代,如何将应用程序的依赖DLL嵌入到EXE文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如这里所述我试图嵌入的dll在exe应用程序,以便只分发一个exe,但是当我试图运行我的应用程序在一个xp机器与完整的.NET 4安装它只是崩溃,没有错误,im放置以下代码在主要方法

  [STAThread] 
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve + =(sender,args)=>
{
String resourceName =AssemblyLoadingAndReflection。 + new AssemblyName(args.Name).Name +.dll;

using(var stream = Assembly.GetExecutingAssembly()。GetManifestResourceStream(resourceName))
{
Byte [] assemblyData = new Byte [stream.Length];
stream.Read(AssemblyData,0,assemblyData.Length);
return Assembly.Load(assemblyData);
}
};

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmrPrincipal());
}

我有一个名为dlls的文件夹,其中im放置

  Functions.Shared.dll 
Alex.UI.dll
Alex.Controls.dll



并将其构建操作设置为嵌入资源。



一段代码,并设置dlls包括点击一次安装程序它工作正常。 btw im使用.NET Framework 4完整配置文件和VS2010 SP1

解决方案

方法。 AssemblyResolve尚未注册,鸡和蛋的问题。您只能使用Main中的类型,因此您必须远离frmrPrincipal。使用一个小助手方法可以解决这个问题,但现在你必须禁止内联[MethodImpl]。你也通过不允许ngen.exe做它的工作拍摄你的脚。

 使用System.Runtime.CompilerServices; 
...
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve + =(sender,args)=>
{
// etc ..
}
AvoidJitterBombing();
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void AvoidJitterBombing(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmrPrincipal());
}


As stated here im trying to embed the dlls in the exe application in order to just distribute one exe, but when i try to run my application on a xp machine with full .NET 4 installed it just crashes with no error, im placing the following code on the main method

[STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll";

                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                {
                    Byte[] assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    return Assembly.Load(assemblyData);
                }
            };

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmrPrincipal());
        }

i have a folder named dlls where im placing

Functions.Shared.dll
Alex.UI.dll
Alex.Controls.dll

and im setting its Build Action to "Embedded Resource".

if i remove that piece of code and set the dlls to be included by click once installer it works fine. btw im Using .NET Framework 4 Full profile and VS2010 SP1

解决方案

The jitter goes kaboom when it tries to jit the Main() method. AssemblyResolve isn't registered yet, chicken and egg problem. You can only use types in Main that are available, so you'll have to stay away from frmrPrincipal. Using a little helper method can solve that problem, but now you'll have to suppress inlining with [MethodImpl]. You are also shooting your foot by not allowing ngen.exe to do its job.

using System.Runtime.CompilerServices;
...
    [STAThread]
    static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {
            // etc..
        }
        AvoidJitterBombing();
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private static void AvoidJitterBombing() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmrPrincipal());
    }

这篇关于ILMerge替代,如何将应用程序的依赖DLL嵌入到EXE文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆