How I can see the Async state machine (async/await under the hood) with the help of DotPeek?
Asked Answered
J

2

2

I'm watching a video lesson, where the author talking about async/await under the hood. 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. 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:

enter image description here

But I don't see code generation result. I see async and await. enter image description here

DotPeek just shows me my source code. 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. Both give me the same result. Am I miss something?

Johanna answered 7/9, 2020 at 19:15 Comment(0)
J
3

So, the answer is simple. Right click on the file -> Decompiled Sources

enter image description here

Johanna answered 7/9, 2020 at 19:52 Comment(0)
S
0

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.

Sodamide answered 2/8, 2021 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.