修复 C# .NET 中由 ILMerge 引起的冲突类型

发布于 2024-10-05 18:43:32 字数 512 浏览 4 评论 0原文

我有一个有趣的问题,我想要一个简单的解决方案。我有一个“库”程序集,在 Visual Studio 解决方案中的“客户端”项目和“测试”项目中都引用了该程序集。问题是测试项目还引用了客户端项目,我们必须使用 ILMerge 将库程序集与客户端程序集合并以进行部署。由于库程序集与客户端程序集合并,因此当测试项目尝试构建时,我会收到有关原始引用的库程序集和合并程序集中都存在库程序集中类型的错误。

真正的问题是我们在客户端项目的构建后步骤中运行 ILMerge;最好的解决方案是将其转移到实际的部署过程中。然而,我们当前的工具将使其难以实现。

有没有一种方法可以告诉 .NET 该类型可能位于多个程序集中,这没关系(考虑到它们实际上是同一个程序集,但在一种情况下只是与另一个程序集合并)?

I have an interesting problem which I would like an easy fix for. I have a "library" assembly that is referenced in both a "client" project and a "test" project in a solution in Visual Studio. The problem is that the test project also references the client project, and we must use ILMerge to merge the library assembly with the client assembly for deployment. Since the library assembly is merged with the client assembly, I get an error about types in my library assembly existing in both the originally referenced library assembly and in the merged assembly when the test project attempts to build.

The real problem is that we have ILMerge running in a post-build step on the client project; the best solution would be to move that to the actual deployment process. However, our current tooling would make that difficult to implement.

Is there a way to tell .NET that the type might be in more than one assembly and that's OK (considering they're actually the same assembly, but just merged with another assembly in one case)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与往事干杯 2024-10-12 18:43:32

因此,如果我理解正确的话,您的测试项目具有对库和客户端的引用,而客户端又将库合并到...因此,在构建测试时,您会获得同一库的两个引用。我认为解决方案是从测试项目中删除库引用,只引用客户端,它将拥有您需要的一切。

So, if i understand it correctly, your test project has a reference to the library and to the client, which in turn has the library merged in...So, at build time for test you get two references of the same library. I think the solution is to remove the library reference from the test project and only reference the client, which will have everything you need.

べ繥欢鉨o。 2024-10-12 18:43:32

如果我理解正确的话,如果您仅在测试中引用合并的程序集,您将可以访问所有类型,从而无需引用库程序集,从而消除 ILMerge 的问题。

您可能想要添加对二进制“客户端”输出(这将是合并文件)的引用,并添加手动构建依赖项以控制正确的编译顺序。

我在我的一个项目中通过手动编辑 CSPROJ 文件来做到这一点,覆盖“CopyFilesToOutputDirectory”目标不仅可以编译,而且可以在构建期间合并“客户端”,但是构建后事件也应该可以解决这个问题(我做了同时发生了一些其他不相关的变化,迫使我改变目标行为)。

然后,我编辑引用合并的 DLL 的其他项目文件以使用如下引用:

<Reference Include="MyMergedLib, Version=1.2.3.4, Culture=neutral, PublicKeyToken=3d58c5c8efc41aa9, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\MyMergedLib\$(OutputPath)MyMergedLib.dll</HintPath>
</Reference>

这确保 VS 始终采用正确的版本(调试/发布)。也许这有帮助。

If I understood correctly, if you were to reference the merged assembly only in your tests, you will get access to all types, making a reference to the library assembly unnecessary, and thus eliminating the problem with ILMerge.

You might want to add a reference to the binary "client" output (which would be the merged file), and add a manual build dependency to control the correct compilation order.

I did this in a project of mine by editing the CSPROJ file manually, overriding the "CopyFilesToOutputDirectory" target to not only compile, but also merge the "client" during the build, but a post build event should also do the trick (I did some other unrelated changes at the same time which forced me to change the target behavior).

I then edited the other project file referencing the merged DLL to use the reference like this:

<Reference Include="MyMergedLib, Version=1.2.3.4, Culture=neutral, PublicKeyToken=3d58c5c8efc41aa9, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\MyMergedLib\$(OutputPath)MyMergedLib.dll</HintPath>
</Reference>

This makes sure that VS always takes the correct version (debug/release). Maybe this helps.

屋檐 2024-10-12 18:43:32

那么,您可以使用 ILLink 的自定义版本(而不是 ILMerge)来解决此问题。

或者,您可以调整它以删除重复的程序集。

请参阅此处的源代码。请注意,ILLink 是一个 C++ 程序。

Well, you could use a customized version of ILLink (instead of ILMerge) to fix this issue.

Or, you could just tweak it to remove the duplicate assembly.

See Source Code here. Note that ILLink is a C++ program..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文