我有一个 C# 库,其中包含我们开发的一些程序集和一些第三方的程序集,我需要将所有内容都放在一个程序集文件中。
当我尝试使用 ILMerge 合并所有内容时,出现以下错误:
An exception occurred during merging:
ILMerge.Merge: The assembly 'TestLibrary' is not marked as containing only managed code. (Consider using the /zeroPeKind option -- but read the documentation first!) em ILMerging.ILMerge.Merge() em ILMerging.ILMerge.Main(String[] args)
当我从合并中删除 TestLibrary 时,它工作正常。但为了让我的项目正常工作,我单独引用了 TestLibrary 并启用 useLegacyV2RuntimeActivationPolicy="true"。
因此,经过更多研究后,我决定尝试使用 Jeffrey Richter 来使用嵌入式资源:
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
String resourceName = "AssemblyLoadingAndReflection." +
new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
它加载资源字节,但在 Assembly.Load 上失败,表明程序格式不正确。
TestLibrary 是一个 C++ .Net v1.0 库。
最佳答案
ILMerge 根本无法处理包含 native 代码的程序集。您必须在使用前在运行时提取 dll,或者传送 dll。 ILMerge using 2 third party dll's C++对此也有解释。
关于c# - 在 .NET v4+ 程序集上嵌入\合并 .NET v1 程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13276481/