如何让 ILSpy 显示编译器生成的代码

时间:2021-03-27 09:57:42

标签: c# compilation syntactic-sugar ilspy

我已经编写并构建了这个应用程序:

namespace Test
{
    
    class Program
    {
        static void Main(string[] args)
        {
            var myClass = new MyClass();
            foreach (var item in myClass.CountFrom(1, 4))
            {
                Console.WriteLine(item);                
            }
            Console.Read();
        }
    }

    public class MyClass
    {
        public IEnumerable<int> CountFrom(int start, int limit)
        {
            for (int i = start; i <= limit; i++)
            {
                yield return i;
            }
        }
    }
}

当我在 ILSpy 中查看代码时,基于 bin/Debug 文件夹中的 .exe,它没有显示我希望看到的状态机代码: enter image description here

如何让它显示 compiler generated code

1 个答案:

答案 0 :(得分:1)

您可以转到“View->Option”并在“Decompiler”选项卡下,取消选中“C#2.0->Decompile enumerator(yield return)”: enter image description here

相关问题