考虑以下示例代码。
如果我在使用Visual Studio 2010(使用CSC编译器)的Debug模式下编译和运行此代码,它将打印出以下结果:
class Program
{
static void Main( string[] args )
{
DoSomethingWithAction( i =>
{
Console.WriteLine( "Value: {0}", i );
} );
Console.ReadLine();
}
private static void DoSomethingWithAction( Action<int> something )
{
Console.WriteLine( something.Target == null
? "Method is static."
: "Method is not static." );
something( 5 );
}
}
如果我在使用Visual Studio 2010(使用CSC编译器)的Debug模式下编译和运行此代码,它将打印出以下结果:
Method is not static.
Value: 5
如果我使用Visual Studio 2010编译相同的代码,但这次使用Release设置,则会生成以下输出:
Method is static.
Value: 5
现在,如果我们使用Visual Studio 2015 CTP(在Roslyn编译器下)执行相同的代码,则对于Debug和Release设置,将生成以下输出:
Method is not static.
Value: 5
首先,我发现在VS2010(CSC)的Debug和Release版本之间存在差异是很奇怪的。为什么在Debug下它不能作为一个静态方法来评估呢?此外,似乎在某些情况下,在Debug中编译后它会被评估为静态的。我有一个正在生产中应用程序在Debug下得到了预期的静态结果。
其次,Roslyn编译器是否应该匹配CSC在这种特定情况下的行为?