两个问题:
1) ILMerge的程序集没有包含基本的.NET程序集
在从.NET 3.5/Visual Studio 2008升级到.NET 4/Visual Studio 2010之后,我在使用ILMerge进行构建后处理时遇到了问题。我有一个解决方案,其中包含几个目标框架设置为".NET Framework 4"的项目。我使用以下ILMerge命令将各个项目DLL合并成单个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将会报“不允许未解决的程序集引用:System”错误。如果我不指定MSTest目录的位置,则会出现“不允许未解决的程序集引用:Microsoft.VisualStudio.QualityTools.UnitTestFramework”错误。
上述ILMerge命令可以工作并生成DLL。然而,当我在另一个.NET 4 C#项目中引用该DLL并尝试使用其中的代码时,我会得到以下警告:
"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,我会得到以下错误:
"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 的人不应该必须添加对其引用的任何库的引用。如何解决这个问题?
2) 只有在使用合并的程序集时才会引发TypeLoadException
编辑:即使在使用MyILMergedDLL.dll的消费者项目中添加了对System.Xml的引用,使用MyILMergedDLL.dll中的一些代码也会导致此异常:
"System.TypeLoadException: Could not load type 'System.Func`2' from assembly 'MyILMergedDLL, Version=1.0.1.1, Culture=neutral, PublicKeyToken=...'."
这是我消费者项目中的代码;引发TypeLoadException的行是第二个:var keys = new[] {"a", "b", "c"};
var row = new Row(keys);
抛出 TypeLoadException
异常的特定 Row
构造函数在 MyILMergedDLL
中的一个公共类中定义,当我在引用单个项目 DLL 时使用此构造函数时,它可以正常工作。只有在引用 IL-merged DLL 时使用此构造函数时,才会出现异常。我不知道发生了什么。
以下是该构造函数:
public Row(IEnumerable<string> keys) : base(keys) { }
它所指的base
具有以下代码:
foreach (string key in keys.Where(
key => !string.IsNullOrEmpty(key)
))
{
_dic.Add(key, string.Empty);
}