简体   繁体   English

“使用”块编译器生成的代码在dotPeek或ILSpy中不可见

[英]'using' block compiler generated code not visible in dotPeek or ILSpy

I am interested to look at the compiler generated code for the using code block which generates try-finally , but I do not see both dotPeek and ILSpy showing this detail. 我有兴趣查看using try-finally代码块生成的using代码块的编译器生成的代码,但是dotPeekILSpy都没有显示此细节。 I used ildasm.exe to look at this code block and I see that it has the try-finally block in it but cannot understand it well...so wanted to see if these 2 tools would help. 我使用ildasm.exe查看此代码块,我发现它具有try-finally块,但无法很好地理解它...所以想看看这两个工具是否有帮助。

Any ideas? 有任何想法吗?

UPDATED: So I recently used a struct which implemented IDisposable in my project and was worried if the using code block and struct with IDisposable would cause boxing...but I later found the following article which mentioned that the compiler optimizes for this situation and does not box when trying to call Dispose. 更新:所以我最近使用它实现了IDisposable在我的项目,很担心,如果一个结构using的代码块和结构与IDisposable的会导致拳击...但我后来发现其中提到编译器对于这种情况,优化并做下面的文章尝试调用“处置”时不显示框。

http://ericlippert.com/2011/03/14/to-box-or-not-to-box/ http://ericlippert.com/2011/03/14/to-box-or-not-to-box/

So I was curious to see what kind of code does the compiler generate for my using block. 因此,我很想知道编译器为我的using块生成什么样的代码。

A simple example repro: 一个简单的示例再现: 在此处输入图片说明

The free JustDecompile tool from Telerik is able to show the details. Telerik提供的免费JustDecompile工具可以显示详细信息。

Basically ( Test being a sample class implementing IDisposable ), the compiled version of: 基本上( Test是实现IDisposable的示例类),其编译版本为:

internal class Program
{
    private static void Main(string[] args)
    {
        using (var test = new Test())
        {
            test.Foo();
        }

        Console.ReadLine();
    }
}

is decompiled to: 反编译为:

internal class Program
{
    public Program()
    {
    }

    private static void Main(string[] args)
    {
        Test test = new Test();
        try
        {
            test.Foo();
        }
        finally
        {
            if (test != null)
            {
                ((IDisposable)test).Dispose();
            }
        }
        Console.ReadLine();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM