码迷,mamicode.com
首页 > Web开发 > 详细

将dll放进exe[.Net]

时间:2015-08-03 06:30:28      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

原文:将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;
        };
    }
}

 

将dll放进exe[.Net]

标签:

原文地址:http://www.cnblogs.com/lonelyxmas/p/4697231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!