装载更名为C#程序集抛出FileNotFoundException异常 [英] Loading renamed C# assembly throws FileNotFoundException

查看:447
本文介绍了装载更名为C#程序集抛出FileNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由C#应用程序中引用的C#组件。

I have an C# assembly that is referenced by a C# application.

由于我们的编码标准,存在调试的DLL由D后缀的规则(如 ProjectA.dll 变成 ProjectAd.dll )。当我重命名的DLL添加到应用程序的引用,它成功地构建,但 FileNotFoundException异常在执行时抛出。

Because of our coding standards, there is a rule where debug DLLs are postfixed by a "d" (e.g. ProjectA.dll becomes ProjectAd.dll). When I add a reference to the renamed DLL to the application, it builds successfully, but throws a FileNotFoundException upon execution.

抛出的错误如下:

System.IO.FileLoadException:无法加载文件或程序集项目A,版本= 1.0.0.0文化=中性公钥= 49df7f988e86ed92'或它的一个依赖。找到的程序集清单定义不匹配程序集引用。 (异常来自HRESULT:0x80131040)
档名:项目A,版本= 1.0.0.0,文化=中性公钥= 49df7f988e86ed92'

装配经理也插话了警告和错误:

The assembly manager also chimes in with a warning and error:

警告:比较程序集名称时发生不匹配:公钥标记结果
错误:未能完成组装(HR = 0x80131040)的设置。探测终止。

从错误信息,它看起来就像是寻找一个集而不 D 后缀。

From the error message, it looks like it is looking for an assembly without the d postfix.

顺便说一下,存在由同一应用程序引用的C / CLI组件。它有一个 D 追加到该DLL,但是观看在VS2005中引用的属性显示,安全标识有 D 正确追加。 C#的引用则的的有 D 在属性窗口中追加。

BTW, there is a C++/CLI assembly that is referenced by the same application. It has a d appended to the DLL, but viewing the properties of the reference in VS2005 shows that the security identity has the d correctly appended. The C# reference does not have the d appended in the properties window.

什么我必须做的就是这个工作正常的调试C#程序集?我试着修改在AssemblyInfo.cs中的条目都无济于事。这是不是一个清单文件将解决?

What do I have to do to get this working properly for the debug C# assemblies? I've tried modifying the entries in AssemblyInfo.cs to no avail. Is this something that a manifest file would resolve?

推荐答案

Unfotunatelly你不能只重命名大会实现这一目标。

Unfotunatelly you cannot achieve this by only renaming the assembly.

程序集的名称是由它编制写进它的元数据。当您以后更改其文件名,你实际上并不在它的元数据更改名称。

The name of an assembly is written into its meta data by its compilation. When you later change its file name you do not actually change the name in its meta data.

然后,通过第二次汇编引用的程序集的名称将读取它元数据和写入到新建组件。

Then by the second compilation the name of the referenced assembly will be read from its meta data and written to the newly built assembly.

在运行时CLR将有关名称所引用的总装基地INT引用组件的元数据搜索。但是,它不会以任何探头路径找到它,并因此将抛出一个异常FileNotFound。

In runtime the CLR will search for the referenced assembly base on the name int the meta data of the referencing assembly. However, it will not find it in any of the probe paths and thus will throw an exception FileNotFound.

您可以通过编辑引用程序集的项目文件,解决这个问题。你这样做,右键单击解决方案资源管理器项目属性并选择卸载项目。然后右键点击卸载项目,并选择编辑项目。第一个标记之前粘贴这一权利的ItemGroup

You can tackle this problem by editing the project file of the referenced assembly. You do that by right clicking the project properties in the solution explorer and selecting unload project. Then right click the unloaded project and select edit project. Paste this right before the first tag ItemGroup

  ... 
  <PropertyGroup>
    <AssemblyName Condition="'$(Configuration)' == 'Debug'">$(AssemblyName)d</AssemblyName>
  </PropertyGroup>
  <ItemGroup>
  ...

这是的MSBuild脚本有条件的AssemblyName属性。当你有你的配置设置调试值,将上述定义的AssemblyName,并添加D到它,它就会只适用。

This is a conditional AssemblyName property of msbuild script. It will be applied only when you have your configuration set to Debug value and will take the above defined AssemblyName and add 'd' to it.

现在你将有名称在这两个文件名和所述元数据d。当您更改回版本配置属性将被省略。

Now you will have the name with 'd' in both the file name and the meta data. When you change back to the Release configuration the property will be omitted.

这篇关于装载更名为C#程序集抛出FileNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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