加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将C#DLL合并到.EXE中

发布时间:2020-12-16 07:05:25 所属栏目:百科 来源:网络整理
导读:我知道有很多回复这个主题,但我在这个回复中找到的示例代码不适用于每个.dll 我使用了this例子. public App() { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly); } static Assembly ResolveAssembly(object sender,R
我知道有很多回复这个主题,但我在这个回复中找到的示例代码不适用于每个.dll

我使用了this例子.

public App()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
    }

    static Assembly ResolveAssembly(object sender,ResolveEventArgs args)
    {
        //We dont' care about System Assemblies and so on...
        if (!args.Name.ToLower().StartsWith("wpfcontrol")) return null;

        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,block.Length);
                return Assembly.Load(block);
            }
        }
        return null;
    }

当我创建一个只有一个窗口的小程序和一个butten它有效,但是我的“大”dll它没有工作. “big”dll上的设置与我的小程序中的设置相同.

我无法想象为什么它有时会起作用,有时它不起作用.我用ICSharp.AvalonEdit.dll测试了它,但没有成功..

任何人都可以想象错误在哪里?

编辑1

当我开始我的程序时,它说它无法找到我的dll.

编辑2

我想我得到了我的问题的核心.如果我要合并的其中一个dll包含对其他dll的引用,那么我将成为此FileNotFoundException异常.有人知道如何加载/添加内部所需的DLL

编辑3

当我使用Ji?íPolá?ek的代码时,它适用于某些人.我的Fluent显示错误“请,附上一个带有样式的ResourceDictionary.但我已经在我的App.xaml中完成了这个

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,/Fluent;Component/Themes/Office2010/Silver.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

解决方法

如果引用的程序集需要其他程序集,则必须将它们与应用程序一起包含在应用程序文件夹或嵌入式资源中.您可以使用Visual Studio,IL Spy,dotPeek确定引用的程序集,或使用方法 Assembly.GetReferencedAssemblies编写自己的工具.

在将ResolveAssembly处理程序附加到它之前,也可能会触发AssemblyResolve事件.附加处理程序应该是您在Main方法中执行的第一件事.在WPF中,您必须使用新的Main方法创建新类,并将其设置为项目设置中的Startup对象.

public class Program
{
    [STAThreadAttribute]
    public static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve
            += new ResolveEventHandler(ResolveAssembly);
        WpfApplication1.App app = new WpfApplication1.App();
        app.InitializeComponent();
        app.Run();
    }

    public static Assembly ResolveAssembly(object sender,ResolveEventArgs args)
    {   
      // check condition on the top of your original implementation
    }
}

您还应该检查ResolveAssembly顶部的保护条件,以排除任何引用的程序集.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读