【发布时间】:2015-03-20 13:05:10
【问题描述】:
我收到了这个错误
System.OverflowException: Arithmetic operation resulted in an overflow.
当我在 .Net 2.0 或更高版本 (.Net 4.0) 中编译的 Windows Server 2008 R2 Standard 上运行我的应用程序时。据我所知,如果 C# 在没有 /checked 参数的情况下编译,则应该忽略算术溢出。在我的应用中有很多地方会发生溢出,所以我需要忽略它。
我追踪了一个例子:
using System;
namespace ArithmeticOverflow
{
class Program
{
static void Main( string[] args )
{
GetTypeID( typeof( Program ) );
}
public static int GetTypeID( Type type )
{
return type.GetHashCode() ^ type.FullName.GetHashCode() ^ type.TypeHandle.Value.ToInt32();
}
}
}
使用 .net 2.0 编译:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe /out:test.exe Program.cs
当我在台式计算机上运行此程序时,一切正常。但是当我在服务器上运行它时,它崩溃了。我找不到问题。
如果我用 .net 1.1 编译它:
C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc.exe /out:test.exe Program.cs
在台式机和服务器上都可以。那么问题出在哪里?请帮忙。
更新
使用 /platform:anycpu32bitpreferred 解决
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe /unsafe /platform:anycpu32bitpreferred /out:test.exe Program.cs
【问题讨论】:
-
你想用逻辑或做什么?
-
@Gilad:
^是 XOR,而不是 OR。 -
@JonSkeet 是的,对不起我的错字。
标签: c# .net arithmetic-expressions