dnSpy showing strange disassembly code for async methods

Viewed 2006

I am trying to disassemble my .NET core 3.1 binary using the latest build of dnSpy but am getting output like this:

enter image description here

The original method:

public async Task<List<FirmwarePackage>> CalculatePackagesDeltaAsync(List<FirmwarePackage> firmwarePackages) {
  var packagesToUpdate = await GetPackageDifferences(firmwarePackages);

  // sort packages
  // "version-update" packages should be updated last, with "mez" being last
  packagesToUpdate = packagesToUpdate
    .OrderBy(x => x.TargetType)
    .ThenBy(x => x.Name)
    .ToList();

  packagesToUpdate.RemoveAll(x => x.Name == "version-update");

  var versionPkgs = firmwarePackages
    .Where(x => x.Name == "version-update")
    .OrderBy(x => x.TargetType);

  packagesToUpdate.AddRange(versionPkgs);
  return packagesToUpdate;
}

Is this normal? How can I make it into something more understandable. Trying to debug this is a nightmare.

On the github repo the issues is not available which is why I am asking here.

2 Answers

What you can see there is totally normal. What you have cropped is just one part of the generated code.

The highlighted code saves the local variables (as fields of the state machine) to preserve their state.

The code generation is quite complex because there are couple of use cases that are handled in the different way. But the generic ideas are written quite well in the following articles:

Stephen Toub has written a blog post which reveals some of the optimizations that the .NET team has accomplished in .NET 5.

For those of you watching at home, echoing @yoelhalb from their comment

Actually it is a setting in Options->Decompiler->C#->Show hidden decompiler generated classes and methods

Related