错误 2 类型 <T>两者都存在......与ILMerge [英] Error 2 The type &lt;T&gt; exists in both... with ILMerge

查看:27
本文介绍了错误 2 类型 <T>两者都存在......与ILMerge的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个项目:

  • A_MainProject(启动项目)
  • B_IntermediateProject
  • C_SharedClassLibrary

依赖是:

A 取决于 B &

A depends on B & C

B 依赖于 C

和 C 仅依赖于系统.

and C depends on System only.

每个项目都有自己的命名空间.

Every Project has it's own namespace.

只要没有安装 ILMerge,一切正常.

As long as no ILMerge is installed, everything works fine.

如果为项目 A(A_MainProject")激活了 ILMerge,一切仍然正常,并且会为项目 A 生成一个独立的可执行文件.

If ILMerge is activated for Project A ("A_MainProject") everything still works fine and one self-contained executable for Project A is generated.

如果现在还为 B_IntermediateProject 激活了 ILMerge,我会收到以下错误:

If ILMerge is now also activated for the B_IntermediateProject, I get the following error:

Error   2   The type 'C_SharedClassLibrary.SharedClass' exists in both 'c:\dev\ILMergeError\B_IntermediateProject\bin\Debug\B_IntermediateProject.exe' and 'c:\dev\ILMergeError\C_SharedClassLibrary\bin\Debug\C_SharedClassLibrary.dll'    C:\dev\ILMergeError\A_MainProject\Program.cs    12  A_MainProject

我将MSBuild ILMerge 任务"1.0.3-rc2 与ILMerge"2.13.0307(来自 mbarnett)一起使用.

I use "MSBuild ILMerge task" 1.0.3-rc2 with "ILMerge" 2.13.0307 (from mbarnett).

最小的解决方案包含 3 个项目,每个项目有一个类.此版本仅在一个项目上激活了 ILMerge 并有效:

The minimal solution contains 3 Projects with one class each. This version has ILMerge activated on only one project and works:

ILMergeError_stillworking.zip

此版本也在项目 B 上激活并产生错误:

This version has it also activated on project B and produces the error:

ILMergeError_error.zip

如果我在外部将两个 exe 和 dll 与 ILMerge (ILMergeGUI) 合并,一切正常.

If I merge the two exes and the dll with ILMerge externally (ILMergeGUI) everything works fine.

提前感谢您的回答,轩坤

Thank you in advance for answering, Xan-Kun

推荐答案

简短的回答是,您应该只在 A 上启用 ILMerge,它会将 A+B+C 合并在一起.

The short answer is that you should only enable ILMerge on A, where it'll merge both A+B+C together.

您遇到此错误的原因是当您在 B 上启用 ILMerge 时,B 也将使用所有 C 类型重新打包(B' = B+C).因此,当 ILMerge 在 A 上触发时,它将合并 A+B'+C = A+(B+C)+C

The reason why you hit this error is that when you enable ILMerge on B, B will be repacked with all C types as well (B' = B+C). Hence when ILMerge triggers on A, it'll merge A+B'+C = A+(B+C)+C

现在可能发生两种情况:

Now 2 scenarios can happen:

  1. B 在内部使用 C,并且不(公开)公开任何 C 类型(例如作为方法参数或返回值).在这种情况下,您可以通过 ILMerge 选项(B 上的/internalize 和 A 上的/allowDup)使其工作,尽管最终合并程序集 A 中的每个 C 类型都会两次.

  1. B uses C internally, and doesn't (publicly) expose any C type (e.g. as method argument or return value). In this case you can make it work through ILMerge options (/internalize on B and /allowDup on A), though you'll end up with twice every C type in the merged assembly A.

B 暴露可能被 A 使用的 C 类型.在这种情况下,即使您设法使合并工作,它也会在运行时失败,因为 A 和 B' 引用不同的类型(例如,对于类 c,B' 引用 B'.c 和 A 引用 Cc),而您会得到令人讨厌的类型转换异常,因为它们是 2 种不同的类型(相同的名称、相同的命名空间,但不同的程序集).

B exposes C types, that are maybe used by A. In this case even if you manage to make the merge work, it'll fail at runtime, because A and B' reference different types (e.g. for a class c, B' references B'.c and A references C.c), And you'll get nasty type cast exceptions, because those are 2 different types (same name, same namespace, but different assemblies).

(它们很讨厌,因为它们没有表明程序集不匹配,并且异常只说c 不能强制转换为 c",这可能会让很多人感到困惑)

(They're nasty because they don't indicate the assembly mismatch, and exception only says that "c cannot be cast to c", which can be confusing to many)

这篇关于错误 2 类型 <T>两者都存在......与ILMerge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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