首页 > Web开发 > 详细

将dll放进exe[.Net]

时间:2015-08-03 06:28:10      阅读:246      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!