将dll放进exe[.Net]

简介: 原文:将dll放进exe[.Net]两种方案: 1、使用ILMerge工具。 缺点:需离开工程,使用第三方工具(ILMerge)。 2、将dll作为Resource放进exe,exe执行时动态加载(Load)Resources内的dll。
原文: 将dll放进exe[.Net]

两种方案:

1、使用ILMerge工具。

缺点:需离开工程,使用第三方工具(ILMerge)。

2、将dll作为Resource放进exe,exe执行时动态加载(Load)Resources内的dll。

缺点:需编写多余代码,加载速度问题。

参考代码:

public partial class App : Application
{
    public App()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {

            Assembly thisAssembly = Assembly.GetExecutingAssembly();

            //Get the Name of the AssemblyFile
            var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";

            //Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder
            var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name));
            if (resources.Count() > 0)
            {
                var resourceName = resources.First();
                using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName))
                {
                    if (stream == null) return null;
                    var block = new byte[stream.Length];
                    stream.Read(block, 0, block.Length);
                    return Assembly.Load(block);
                }
            }
            return null;
        };
    }
}

 

目录
相关文章
|
2月前
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
mvc.net分页查询案例——DLL数据访问层(HouseDLL.cs)
9 0
|
小程序 C# Windows
|
开发框架 .NET 开发工具
检查.net dll构建的目标平台是any cpu、x86、x64
原文:检查.net dll构建的目标平台是any cpu、x86、x64 有时候,需要检查构建的dll是否针对正确的平台 可以使用CorFlags.exe(它是.NET Framework SDK的一部分)从dll中查找此信息。
1202 0
|
C# C++ 开发工具
NET Core 生成Hello world.exe
.NET Core 生成Hello world.exe 本文描述如何用VS CODE生成控制台程序,然后发布成.exe可执行文件. 材料: Win7系统电脑一台; 安装VS Code; 安装VS Code C# 插件; 安装.
2933 0