简体   繁体   English

如何在 DotPeek 的帮助下查看异步状态机(引擎盖下的异步/等待)?

[英]How I can see the Async state machine (async/await under the hood) with the help of DotPeek?

I'm watching a video lesson, where the author talking about async/await under the hood.我正在观看视频课程,其中作者在幕后谈论 async/await。 He shows prepared and decompiled code.他展示了准备好的和反编译的代码。 I want to do the same from scratch.我想从头开始做同样的事情。 I mean, decompile some C# compiled file with the help of, from example, DotPeek.我的意思是,在例如 DotPeek 的帮助下反编译一些 C# 编译文件。 So I have the following simple example:所以我有以下简单的例子:

class Program
{
    public static async Task KekAsync()
    {
        Console.WriteLine("Current thread id before await {0}", Thread.CurrentThread.ManagedThreadId);
        await Task.Delay(200);
        Console.WriteLine("Current thread id after await {0}", Thread.CurrentThread.ManagedThreadId);
        await Task.Delay(200);
        Console.WriteLine("Current thread id after await again {0}", Thread.CurrentThread.ManagedThreadId);
        await Task.Delay(200);
        Console.WriteLine("Current thread id after await again and again {0}", Thread.CurrentThread.ManagedThreadId);

    }

    static async Task Main(string[] args)
    {
        await KekAsync();
    }
}

In the DotPeek settings I have the following:在 DotPeek 设置中,我有以下内容:

在此处输入图片说明

But I don't see code generation result.但我没有看到代码生成结果。 I see async and await.我看到异步和等待。 在此处输入图片说明

DotPeek just shows me my source code. DotPeek 只是向我展示了我的源代码。 But I would like to see implementation of Async state machine.但我想看到异步状态机的实现。 Result of the code generation.代码生成结果。 I used .NET Core 3.1 and the last version of .NET Framework.我使用了 .NET Core 3.1 和最新版本的 .NET Framework。 Both give me the same result.两者都给我相同的结果。 Am I miss something?我错过了什么吗?

So, the answer is simple.所以,答案很简单。 Right click on the file -> Decompiled Sources右键单击文件 -> 反编译源

在此处输入图片说明

Because < and > characters within names are invalid the generated source will not compile.由于名称中的 < 和 > 字符无效,因此生成的源代码将无法编译。

But with the help of dnSpy you can rename those generated classes, fields and methods to get compilable source code for further investigation.但是在 dnSpy 的帮助下,您可以重命名这些生成的类、字段和方法,以获得可编译的源代码以供进一步调查。

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

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