其中一个DLL作为一个WPF项目中嵌入的资源 [英] Including a DLL as an Embedded Resource in a WPF project

查看:862
本文介绍了其中一个DLL作为一个WPF项目中嵌入的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的 http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-第三edition.aspx

我添加到WPFToolkit.Extended.dll我的解决方案,并将其生成操作嵌入的资源。

I've added WPFToolkit.Extended.dll to my solution, and set its Build Action to Embedded Resource.

在App.OnStartup(StartupEventArgs E)我有以下代码:

In App.OnStartup(StartupEventArgs e) I have the following code:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
    String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll";
    String assemblyName = Assembly.GetExecutingAssembly().FullName;
    Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
    using (stream)
    {
        Byte[] assemblyData = new Byte[stream.Length];
        stream.Read(assemblyData, 0, assemblyData.Length);
        return Assembly.Load(assemblyData);
    }
};



调试器打这个代码块的两倍。

The debugger hits this block of code twice.

第一次:

resourceName is "AssemblyLoadingAndReflection.StatusUtil.resources.dll"
assemblyName is "StatusUtil, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
stream is null

第二次:

resourceName is "AssemblyLoadingAndReflection.WPFToolkit.Extended.resources.dll"
assemblyName is "StatusUtil, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
stream is null



代码抛出一个异常,当它击中stream.Length,因为它是空。

The code throws an exception when it hits stream.Length, since it's null.

因为它是一个WPF项目,我不能使用ILMerge。

I can't use ILMerge because it's a WPF project.

推荐答案

您必须更改字符串AssemblyLoadingAndReflection你的应用程序集的名称。

You have to change the string "AssemblyLoadingAndReflection" to the name of your application assembly.

有一件事你可以做,使这个代码更通用通过使用一些更多的思考:

One thing you can do to make this code more generic by using some more reflection:

Assembly.GetExecutingAssembly().FullName.Split(',').First()

不要忘记追加一个点。这当然如果DLL是不是在应用程序组装的资源无法工作。

Don't forget to append a dot. This will of course not work if the dll is not in the resources of the application assembly.

这篇关于其中一个DLL作为一个WPF项目中嵌入的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆