C#编译器错误地优化code [英] C# Compiler Incorrectly Optimizes Code

查看:194
本文介绍了C#编译器错误地优化code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个远程Web服务器上运行的ASP.NET应用程序,我刚开始收到此错误:

I have a ASP.NET application running on a remote web server and I just started getting this error:

Method not found: 'Void System.Collections.Generic.ICollection`1..ctor()'.

我拆开DLL中的code和它看起来像编译器错误地优化code。 (请注意,设置是实现一组唯一对象的类它自IEnumerable继承。)这行:

I disassembled the code in the DLL and it seems like the compiler is incorrectly optimizing the code. (Note that Set is a class that implements a set of unique objects. It inherits from IEnumerable.) This line:

Set<int> set = new Set<int>();

被编译进这一行:

Is compiled into this line:

Set<int> set = (Set<int>) new ICollection<CalendarModule>();

该CalendarModule类是完全无关的类!有没有人注意过.NET编译错误code这样过吗?

The CalendarModule class is a totally unrelated class!! Has anyone ever noticed .NET incorrectly compiling code like this before?

更新#1:这个问题似乎是微软的介绍ILMerge 工具。目前,我们正在研究如何克服它。

Update #1: This problem seems to be introduced by Microsoft's ILMerge tool. We are currently investigating how to overcome it.

更新#2::我们发现两种方法至今来解决这个问题。我们不明白的根本问题是什么,但是这两种解决它:

Update #2: We found two ways to solve this problem so far. We don't quite understand what the underlying problem is, but both of these fix it:


  1. 关闭优化。

  1. Turn off optimization.

合并与ILMerge的assemblie在不同的机器上。

Merge the assemblie with ILMerge on a different machine.

因此​​,我们在想,如果构建机上以某种方式(这是奇怪的考虑,我们一直在使用的机器,现在建了一年多的版本),或者是一些其他的问题。

So we are left wondering if the build machine is misconfigured somehow (which is strange considering that we have been using the machine to build releases for over a year now) or if it is some other problem.

推荐答案

啊,ILMerge - 在你的问题额外的信息确实与您的问题有所帮助。虽然我不指望过的.NET编译器这样,我会希望偶尔能看到这样的事情与ILMerge失败(因为它在做什么)。

Ahh, ILMerge - that extra info in your question really helps with your problem. While I wouldn't ever expect the .net compiler to fail in this way I would expect to occasionally see this sort of thing with ILMerge (given what it's doing).

我的猜测是,你的两个组件都使用相同的优化绝招,而合并后你会得到冲突。

My guess is that two of your assemblies are using the same optimisation 'trick', and once merged you get the conflict.

你有没有提出与微软的bug?

Have you raised the bug with Microsoft?

在此期间,一种解决方法是重新编译从源代码组件作为一个部件,节省ILMerge的需要。由于的csproj文件只是XML名单,他们基本上易合并,你可以自动化作为一个额外的MSBuild的步骤。

A workaround in the meantime is to recompile the assemblies from source as a single assembly, saving the need for ILMerge. As the csproj files are just XML lists they're basically easy to merge, and you could automate that as an extra MSBuild step.

这篇关于C#编译器错误地优化code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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