在哪里可以了解 VS 调试器的魔法名称

如果您曾经使用反射器,您可能注意到 C # 编译器生成类型、方法、字段和局部变量,这些值得调试器进行“特殊”显示。例如,以“ CS $”开头的局部变量不会显示给用户。对于匿名方法的闭包类型、自动属性的后台字段等等,还有其他特殊的命名约定。

我的问题是: 在哪里学习这些命名约定? 有人知道一些文档吗?

My objective is to make PostSharp 2.0 use the same conventions.

12414 次浏览

这些都是编译器没有文档记录的实现细节,随时可能发生变化。(更新: 请参见: http://github.com/dotnet/roslyn/blob/master/src/Compilers/CSharp/Portable/Symbols/Synthesized/GeneratedNames.cs”rel = “ noReferrer”> GeneratedNames.cs 下面的描述有点过时了。)

然而,因为我是一个好人,这里有一些细节:

If you have an unused local variable that the optimizer removes, we emit debug info for it anyway into the PDB. We stuck the suffix __Deleted$ onto such variables so that the debugger knows that they were in source code but not represented in the binary.

编译器分配的临时变量槽被命名为 CS $X $Y 模式,其中 X 是“临时类型”,Y 是迄今为止分配的临时变量的数量。临时类别包括:

0 --> short lived temporaries
1 --> return value temporaries
2 --> temporaries generated for lock statements
3 --> temporaries generated for using statements
4 --> durable temporaries
5 --> the result of get enumerator in a foreach
6 --> the array storage in a foreach
7 --> the array index storage in a foreach.

8到264之间的临时类型是用于多维数组的附加数组索引存储。

高于264的临时类型用于包含修复字符串的固定语句的临时类型。

Special compiler-generated names are generated for:

1 --> the iterator state ("state")
2 --> the value of current in an iterator ("current")
3 --> a saved parameter in an iterator
4 --> a hoisted 'this' in an iterator ("this")
5 --> a hoisted local in an iterator
6 --> the hoisted locals from an outer scope
7 --> a hoisted wrapped value ("wrap")
8 --> the closure class instance ("locals")
9 --> the cached delegate instance ("CachedAnonymousMethodDelegate")
a --> the iterator instance ("iterator")
b --> an anonymous method
c --> anonymous method closure class ("DisplayClass")
d --> iterator class
e --> fixed buffer struct ("FixedBuffer")
f --> anonymous type ("AnonymousType")
g --> initializer local ("initLocal")
h --> query expression temporary ("TransparentIdentifier")
i --> anonymous type field ("Field")
j --> anonymous type type parameter ("TPar")
k --> auto prop field ("BackingField")
l --> iterator thread id
m --> iterator finally ("Finally")
n --> fabricated method ("FabricatedMethod")
o --> dynamic container class ("SiteContainer")
p --> dynamic call site ("Site")
q --> dynamic delegate ("SiteDelegate")
r --> com ref call local ("ComRefCallLocal")
s --> lock taken local ("LockTaken")

生成魔法名称的模式是: P<N>C__SI,其中:

  • 对于缓存的委托和显示类实例,P 为 CS $,否则为空。
  • N 是与该事物关联的原始名称(如果有的话)
  • C 是上面列出的字符1到 s
  • S 是一个描述性后缀(“当前”、“状态”等) ,这样在读取元数据时就不必记住上面的表。
  • I 是一个可选的唯一数