异步代码中堆栈中的 d__Xa 部分到底是什么?

What is exactly the d__Xa part in stack in async code?

当我在异常堆栈中有这一行时:

WebApiAccessService.<GetStudentKey>d__14a.MoveNext()

什么是"d__14a"?它是否可以告诉我(没有符号)从方法开始的一些偏移量?

具有奇怪名称的堆栈跟踪告诉您异常是在 <GetStudentKey>d__14a 类型的 MoveNext() 方法中抛出的。

当您使用 async 和 await 来实现状态机时。这个状态机涉及一个结构,在你的例子中,结构的名称是 <GetStudentKey>d__14a。显然,这不是 C# 中结构的合法名称,即使它在 IL 中工作正常,但这正是编译器使用保证不会与您自己创建的任何名称冲突的名称的原因。

您将不得不查看 GeneratedNames class 中的 compiler source code to understand how the d__14a part is generated. Most likely the logic is in the method MakeMethodScopedSynthesizedName 但算法的确切性质应该不重要。

编译器将为自动属性、lambda 表达式、匿名类型、迭代器块等创建类似的 "invalid" 名称