Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';我看不出编译器如何使用他为我的闭包创建的类_C#_Compiler Construction_Reflector - Fatal编程技术网

C# Can';我看不出编译器如何使用他为我的闭包创建的类

C# Can';我看不出编译器如何使用他为我的闭包创建的类,c#,compiler-construction,reflector,C#,Compiler Construction,Reflector,我编写了这个非常基本的程序来检查编译器在幕后做了什么: class Program { static void Main(string[] args) { var increase = Increase(); Console.WriteLine(increase()); Console.WriteLine(increase()); Console.ReadLine(); } static Func&

我编写了这个非常基本的程序来检查编译器在幕后做了什么:

class Program
{
    static void Main(string[] args)
    {
        var increase = Increase();
        Console.WriteLine(increase());
        Console.WriteLine(increase());
        Console.ReadLine();
    }

    static Func<int> Increase()
    {
        int counter = 0;
        return () => counter++;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var增加=增加();
控制台写入线(增加());
控制台写入线(增加());
Console.ReadLine();
}
静态函数增加()
{
int计数器=0;
return()=>counter++;
}
}
现在,当我使用Reflector查看代码时,我确实看到编译器为我的闭包生成了一个类,如下所示:

[CompilerGenerated]
private sealed class <>c__DisplayClass1
{
    // Fields
    public int counter;

    // Methods
    public int <Increase>b__0()
    {
        return this.counter++;
    }
}
private static Func<int> Increase()
{
    int counter = 0;
    return delegate {
        return counter++;
    };
}
[编译生成]
专用密封c类显示器Class1
{
//田地
公共int计数器;
//方法
公共int b__0()
{
返回这个.counter++;
}
}
那很好,我知道他需要这样做来处理我的结案。然而,我看不到的是他实际上是如何使用这个类的。我的意思是我应该能够在某个地方找到实例化“c__DisplayClass1”的代码,我错了吗

编辑

如果单击“增加”方法,它看起来如下所示:

[CompilerGenerated]
private sealed class <>c__DisplayClass1
{
    // Fields
    public int counter;

    // Methods
    public int <Increase>b__0()
    {
        return this.counter++;
    }
}
private static Func<int> Increase()
{
    int counter = 0;
    return delegate {
        return counter++;
    };
}
private static Func Increase()
{
int计数器=0;
返回代表{
返回计数器++;
};
}

您应该在
增加
方法中找到它,我希望该方法能够实现以下内容:

// Not actually valid C# code because of the names...
static Func<int> Increase()
{
    <>c__DisplayClass1 closure = new c__DisplayClass1();
    closure.counter = 0;
    return new Func<int>(closure.<Increase>b__0);
}
//由于名称原因,C代码实际上无效。。。
静态函数增加()
{
c_uudisplayclas1闭包=新的c_udisplayclas1();
closure.counter=0;
返回新的Func(closure.b___0);
}

Reflector不会向您显示该代码,除非您关闭它的优化,但它应该在那里。要么关闭Reflector的优化,要么使用ildasm。

您可以发布其余的编译代码吗?尤其是你的主要方法。@dowhilefor:主要方法实际上会很无聊。它只是调用一个方法来获取一个委托,调用该委托几次并打印结果,然后调用
Console.ReadLine
。我希望它看起来像这样,但事实并非如此。我需要一个商业版的反射器才能看到它吗?很抱歉,这是一个垃圾场,但我该如何优化它呢?我找不到它。顺便说一句,我使用的是Resharper 6.8.2.5。@Cristoph:关闭Reflector的优化功能通常对我很有效(或者将级别设置为.NET 1.1)。你试过只看IL吗?@Cristoph:在我使用的版本中,在语言选择框的右边有一个下拉列表。在我的版本中,它隐藏在选项中,但它确实存在。如果我把它调整到.NET1.0,我就会看到它是如何被使用的。但只是为了把它做好。当您针对不同的.NET版本时,编译器会生成不同的代码吗?