代码之家  ›  专栏  ›  技术社区  ›  David Klempfner

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

  •  0
  • David Klempfner  · 技术社区  · 5 年前

    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;
                }
            }
        }
    }
    

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

    我怎样才能让它显示 compiler generated code

    1 回复  |  直到 5 年前
        1
  •  1
  •   vernou    5 年前

    您可以转到“查看->选项”,在“反编译器”选项卡下,取消选中“C#2.0->反编译枚举器(yield return)”: enter image description here

    推荐文章