CSC 和 Roslyn 编译器的静态 lambda 表达式评估的区别?
Difference in CSC and Roslyn compiler's static lambda expression evaluation?
考虑以下示例代码。
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[=32= 生成以下输出] 和 发布 设置:
Method is not static.
Value: 5
首先,我很好奇 VS2010 (CSC) 的 Debug 和 Release 版本之间存在差异。 为什么它在调试下不评估为静态方法?此外,在某些情况下,它似乎在调试中编译时评估为静态方法。我有一个生产应用程序在调试下获得预期的静态结果。
其次,在这种特殊情况下,Roslyn 编译器是否应该匹配 CSC 的行为?
这是 Roslyn 团队有意做出的更改。
指向实例方法的委托调用速度稍微快一些,因此 Roslyn 现在将 lambda 编译为实例方法,即使在不需要时也是如此。
考虑以下示例代码。
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[=32= 生成以下输出] 和 发布 设置:
Method is not static.
Value: 5
首先,我很好奇 VS2010 (CSC) 的 Debug 和 Release 版本之间存在差异。 为什么它在调试下不评估为静态方法?此外,在某些情况下,它似乎在调试中编译时评估为静态方法。我有一个生产应用程序在调试下获得预期的静态结果。
其次,在这种特殊情况下,Roslyn 编译器是否应该匹配 CSC 的行为?
这是 Roslyn 团队有意做出的更改。
指向实例方法的委托调用速度稍微快一些,因此 Roslyn 现在将 lambda 编译为实例方法,即使在不需要时也是如此。