差异CSC和罗斯林编译器的静态lambda表达式的评价? [英] Difference in CSC and Roslyn compiler's static lambda expression evaluation?

查看:264
本文介绍了差异CSC和罗斯林编译器的静态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编译器),它会打印出以下结果:

If I compile and run this code under Debug using the Visual Studio 2010 (under CSC compiler) it will print out the following result:

Method is not static.
Value: 5

如果我编译在Visual Studio 2010中相同的代码,但是这一次使用的发布设置,将产生以下输出:

If I compile the same code in Visual Studio 2010, but this time using Release settings, the following output will be generated:

Method is static.
Value: 5

现在,如果我们执行相同的代码,但这次使用的Visual Studio 2015 CTP(在罗斯林下编译),是您在调试发布设置生成以下的输出:

Now, if we were to execute the same code but this time using Visual Studio 2015 CTP (under the Roslyn compiler), the following output is generated for both Debug and Release settings:

Method is not static.
Value: 5



首先,我觉得很奇怪,有调试之间的差异和发布VS2010(CSC)的版本。 它为什么不计算为在调试一个静态方法?同时,似乎在某些情况下,在调试编译时,它评估为静态的。我有越来越调试下,预计静态结果,生产应用程序。

First, I find it curious that there is a difference between the Debug and Release versions of VS2010 (CSC). Why would it not evaluate as a static method under debug? Also, it appears that under some cases it does evaluate as static when compiled in Debug. I have a production application that is getting the expected static result under Debug.

其次,应在罗斯林编译器CSC的行为符合在这种特殊情况下<? / STRONG>

Secondly, should the Roslyn compiler match the behavior of CSC in this particular case?

推荐答案

这是由罗斯林队取得了刻意改变。

This was a deliberate change made by the Roslyn team.

委托是稍快调用,所以现在罗丝琳lambda表达式编译为实例方法,即使它并不需要。

Delegates that point to instance methods are slightly faster to invoke, so Roslyn now compiles lambdas to instance methods even when it doesn't need to.

请参阅< A HREF =https://roslyn.codeplex.com/workitem/246>讨论。

这篇关于差异CSC和罗斯林编译器的静态lambda表达式的评价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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