c# - 将 C# DLL 合并到 .EXE

标签 c# wpf dll merge fluent

我知道这个主题有很多回复,但是我在这个回复中找到的示例代码不适用于每个 .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, 0, block.Length);
                return Assembly.Load(block);
            }
        }
        return null;
    }

当我创建一个只有一个窗口和一个按钮的小程序时它可以工作,但是对于我的“大”dll 它没有工作。 “大”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 编写您自己的工具.

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

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 顶部的保护条件,以不排除任何引用的程序集。

关于c# - 将 C# DLL 合并到 .EXE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18014543/

相关文章:

wpf - 如何使用 INotifyPropertyChanged 更新数组绑定(bind)?

c# - 模块窗口中缺少项目 dll

c# - WPF - 将参数从对话框窗口传递给用户控件

c# - 将交错数组转换为多维数组

wpf - XAML - 如何拥有全局 inputBindings?

WPF、MVVM 和异步工作

c++ - 我可以暂停除一个线程之外的进程吗?

c++ - 使用静态链接编译 VS2015

c# - 优雅的方式解析URL

c# - linq 无法对 'Table(req)' 执行创建、更新或删除操作,因为它没有主键