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

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

问题描述

两个问题:

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

在从.NET 3.5 / Visual Studio 2008升级到.NET 4 / Visual Studio 2010后,我的后期制作中使用ILMerge有困难。我有一个解决方案,其目标框架设置为。 .NET Framework 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)\Microsoft\ILMerge\ILMerge.exe"
    "C:\Program Files (x86)\Microsoft\ILMerge\ILMerge.exe"
      /lib:"C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
      /lib:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies"
      /keyfile:"$(SolutionDir)$(SolutionName).snk"
      /targetplatform:v4
      /out:"$(SolutionDir)bin\development\$(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无法解析,因为它具有间接依赖于.NET Framework程序集mscorlib,Version = 4.0,Culture = neutral,PublicKeyToken = b77a5c561934e089,它的版本4.0.65535.65535比当前目标框架中的版本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:无法从程序集加载类型'System.Func`2' 'MyILMergedDLL,Version = 1.0.1.1,Culture = neutral,PublicKeyToken = ...'。

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);

特定的构造函数, code> TypeLoadException 在 MyILMergedDLL 中的一个公共类中定义,当我在引用单个项目DLL时使用此构造函数时,它将工作精细。只有当我使用这个构造函数引用IL-merge 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) { }

code> 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

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:\Windows\Microsoft.NET\Framework64\v4.0.30319选项非常错误。在.NET 4.5发布之后,最近很多程序员遇到麻烦。该目录是不是适用于.NET 4.0参考程序集的目录。它的内容被4.5程序集覆盖,您不能再使用它来定位.NET 4.0安装。你得到的运行时错误非常尴尬,程序找不到某些类型了。通常在[扩展]属性上轰炸,有时在ICommand界面上。

Addendum. There's something very, very wrong about your /lib:"C:\Windows\Microsoft.NET\Framework64\v4.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 Assemblies\Microsoft\Framework\.NETFramework\v4.0"

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

Adjust to match your particular machine and target framework version.

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

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