C#编译器不正确地优化代码 [英] C# Compiler Incorrectly Optimizes Code

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

问题描述

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

 发现:'Void System.Collections.Generic.ICollection`1..ctor()'。 

我反汇编DLL中的代码,似乎编译器不正确地优化代码。 (注意,Set是一个实现一组唯一对象的类,它继承自IEnumerable。)这行:

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

编译到此行:

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

CalendarModule类是一个完全不相关的类!有没有人注意到.NET错误地编译这样的代码?



更新#1:这个问题似乎是由微软的 ILMerge 工具。



更新#2:到目前为止,我们找到了两种方法来解决这个问题。我们不太明白底层问题是什么,但是这两个问题都解决了:


  1. 关闭优化。 p>


  2. 在不同的机器上将装配与ILMerge合并。


所以我们还在想,如果构建机器是错误配置的某种方式(这是奇怪的,考虑到我们已经使用机器来生成版本一年多了),或者如果它是一些其他问题。

解决方案

Ahh,ILMerge - 你的问题中的额外信息真的帮助你的问题。虽然我不会期望.net编译器以这种方式失败,我希望偶尔看到这种事情与ILMerge(考虑到它正在做什么)。



我猜你的两个程序集都使用相同的优化技巧,一旦合并就会产生冲突。



你是否提出了Microsoft的错误? p>

同时,一个解决方法是将源文件中的程序集重新编译为单个程序集,从而节省ILMerge的需要。由于csproj文件只是XML列表,它们基本上很容易合并,您可以将其作为一个额外的MSBuild步骤自动化。


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()'.

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>();

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

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

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. Turn off optimization.

  2. 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.

解决方案

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.

Have you raised the bug with Microsoft?

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#编译器不正确地优化代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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