将 ILMerge 与 .NET 4 库一起使用 [英] using ILMerge with .NET 4 libraries

查看:21
本文介绍了将 ILMerge 与 .NET 4 库一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题:

1) 基本 .NET 程序集不包含在 ILMerged 程序集中

从 .NET 3.5/Visual Studio 2008 升级到 .NET 4/Visual Studio 2010 后,我在后期构建中使用 ILMerge 时遇到问题.我有一个解决方案,其中包含多个目标框架设置为.NET"的项目框架 4".我使用以下 ILMerge 命令将各个项目 DLL 合并为一个 DLL:

I'm having trouble using ILMerge in my post-build after upgrading from .NET 3.5/Visual Studio 2008 to .NET 4/Visual Studio 2010. I have a Solution with several projects whose target framework is set to ".NET Framework 4". I use the following ILMerge command to merge the individual project DLLs into a single DLL:

if not $(ConfigurationName) == Debug
  if exist "C:Program Files (x86)MicrosoftILMergeILMerge.exe"
    "C:Program Files (x86)MicrosoftILMergeILMerge.exe"
      /lib:"C:WindowsMicrosoft.NETFramework64v4.0.30319"
      /lib:"C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEPublicAssemblies"
      /keyfile:"$(SolutionDir)$(SolutionName).snk"
      /targetplatform:v4
      /out:"$(SolutionDir)bindevelopment$(SolutionName).dll"
      "$(SolutionDir)Connection$(OutDir)Connection.dll"
      ...other project DLLs...
      /xmldocs 

如果我不指定 .NET 4 框架目录的位置,我会从 ILMerge 收到不允许未解析的程序集引用:系统"错误.如果我不指定 MSTest 目录的位置,我会收到不允许未解析的程序集引用:Microsoft.VisualStudio.QualityTools.UnitTestFramework"错误.

If I leave off specifying the location of the .NET 4 framework directory, I get an "Unresolved assembly reference not allowed: System" error from ILMerge. If I leave off specifying the location of the MSTest directory, I get an "Unresolved assembly reference not allowed: Microsoft.VisualStudio.QualityTools.UnitTestFramework" error.

上面的 ILMerge 命令可以工作并生成一个 DLL.但是,当我在另一个 .NET 4 C# 项目中引用该 DLL 并尝试在其中使用代码时,我收到以下警告:

The ILMerge command above works and produces a DLL. When I reference that DLL in another .NET 4 C# project, however, and try to use code within it, I get the following warning:

无法解析主要引用MyILMergedDLL",因为它间接依赖于具有更高版本4.0.65535.65535"的 .NET Framework 程序集mscorlib, Version=4.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"比当前目标框架中的4.0.0.0"版本.

The primary reference "MyILMergedDLL" could not be resolved because it has an indirect dependency on the .NET Framework assembly "mscorlib, Version=4.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.65535.65535" than the version "4.0.0.0" in the current target framework.

如果我然后删除 /targetplatform:v4 标志并尝试使用 MyILMergedDLL.dll,我会收到以下错误:

If I then remove the /targetplatform:v4 flag and try to use MyILMergedDLL.dll, I get the following error:

类型System.Xml.Serialization.IXmlSerializable"是在未引用的程序集中定义的.您必须添加对程序集System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"的引用.

The type 'System.Xml.Serialization.IXmlSerializable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

看来我不应该这样做.使用我的 MyILMergedDLL.dll API 的人不应该添加对它引用的任何库的引用.我该如何解决这个问题?

It doesn't seem like I should have to do that. Whoever uses my MyILMergedDLL.dll API should not have to add references to whatever libraries it references. How can I get around this?

2) TypeLoadException 仅在使用合并程序集时

除此之外,即使我在使用 MyILMergedDLL.dll 的消费者项目中添加了对 System.Xml 的引用,也使用了 MyILMergedDLL 中的一些代码.dll 给出了这个异常:

beyond this, even if I do add a reference to System.Xml in the consumer project that uses MyILMergedDLL.dll, making use of some code in MyILMergedDLL.dll gives this exception:

System.TypeLoadException: 无法从程序集MyILMergedDLL, Version=1.0.1.1, Culture=neutral, PublicKeyToken=..."加载类型System.Func`2".

System.TypeLoadException: Could not load type 'System.Func`2' from assembly 'MyILMergedDLL, Version=1.0.1.1, Culture=neutral, PublicKeyToken=...'.

这是我的消费者项目中的代码;导致 TypeLoadException 的行是第二个:

This is the code in my consumer project; the line that caused the TypeLoadException is the second one:

var keys = new[] {"a", "b", "c"};
var row = new Row(keys);

抛出 TypeLoadException 的特定 Row 构造函数是在 MyILMergedDLL 的公共类中定义的,当我在引用单个项目 DLL,它工作正常.只有当我在引用 IL 合并的 DLL 时使用此构造函数时,才会出现异常.我不知道发生了什么.

The particular Row constructor that throws the TypeLoadException is defined in a public class in MyILMergedDLL, and when I use this constructor when referencing the individual project DLLs, it works fine. It is only when I use this constructor when referencing the IL-merged DLL that I get the exception. I don't know what's going on.

这是构造函数:

public Row(IEnumerable<string> keys) : base(keys) { }

并且它所引用的 base 具有以下代码:

And the base to which it is referring has this code:

foreach (string key in keys.Where(
    key => !string.IsNullOrEmpty(key)
))
{
    _dic.Add(key, string.Empty);
}

推荐答案

有一个 最新版本 用于解决 x64 问题.如果仍有问题,请直接与 Mike Barnett 联系 (mbarnett at microsoft dot com)

There was a very recent release to solve x64 problems. Get in touch with Mike Barnett directly if you still have problems (mbarnett at microsoft dot com)

附录.您的 /lib:"C:WindowsMicrosoft.NETFramework64v4.0.30319" 选项有一些非常非常错误的地方.最近,在 .NET 4.5 发布后,这让很多程序员陷入困境.该目录不是适合 .NET 4.0 参考程序集的目录.它的内容被 4.5 程序集覆盖,您不能再使用它来定位 .NET 4.0 安装.你得到的运行时错误很尴尬,程序再也找不到某些类型了.通常轰炸[Extension]属性,有时轰炸ICommand界面.

Addendum. There's something very, very wrong about your /lib:"C:WindowsMicrosoft.NETFramework64v4.0.30319" option. This has been getting lots of programmers in trouble lately, after .NET 4.5 was released. That directory is not the proper one for .NET 4.0 reference assemblies. Its content gets overwritten with the 4.5 assemblies, you can no longer use it to target a .NET 4.0 install. The runtime error you get is very awkward, the program cannot find certain types anymore. Usually bombing on the [Extension] attribute, sometimes on the ICommand interface.

这些类型以及其他一些类型从一个程序集移动到另一个程序集.使用正确的参考组件是一项非常严格的要求.您必须使用:

These types, and some others, were moved from one assembly to another. Using the correct reference assemblies is a rock-hard requirement. You must use:

 /lib:"C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0"

调整以匹配您的特定机器和目标框架版本.

Adjust to match your particular machine and target framework version.

这篇关于将 ILMerge 与 .NET 4 库一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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