问题描述
如果您曾经使用过 Reflector,您可能会注意到 C# 编译器生成的类型、方法、字段和局部变量值得调试器特殊"显示.例如,以CS$"开头的局部变量不会显示给用户.对于匿名方法的闭包类型、自动属性的支持字段等还有其他特殊的命名约定.
If you've ever used Reflector, you probably noticed that the C# compiler generates types, methods, fields, and local variables, that deserve 'special' display by the debugger. For instance, local variables beginning with 'CS$' are not displayed to the user. There are other special naming conventions for closure types of anonymous methods, backing fields of automatic properties, and so on.
我的问题:在哪里可以了解这些命名约定?有人知道一些文档吗?
My question: where to learn about these naming conventions? Does anyone know about some documentation?
我的目标是让 PostSharp 2.0 使用相同的约定.
My objective is to make PostSharp 2.0 use the same conventions.
推荐答案
这些是编译器未记录的实现细节,可能随时更改.(更新:见 GeneratedNames.cs
在 C# 源代码中获取当前详细信息;下面的描述有些过时了.)
These are undocumented implementation details of the compiler, and subject to change at any time. (UPDATE: See GeneratedNames.cs
in the C# sources for the current details; the description below is somewhat out-of-date.)
但是,由于我是个好人,以下是其中的一些细节:
However, since I'm a nice guy, here are some of those details:
如果优化器删除了未使用的局部变量,我们无论如何都会将调试信息发送到 PDB 中.我们将后缀 __Deleted$
附加到这些变量上,以便调试器知道它们在源代码中但未在二进制文件中表示.
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 是到目前为止分配的临时变量的数量.临时种类是:
Temporary variable slots allocated by the compiler are given names with the pattern CS$X$Y, where X is the "temporary kind" and Y is the number of temporaries allocated so far. The temporary kinds are:
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 之间的临时种类是多维数组的附加数组索引存储.
Temporary kinds between 8 and 264 are additional array index storages for multidimensional arrays.
264 以上的临时种类用于涉及固定字符串的固定语句的临时种类.
Temporary kinds above 264 are used for temporaries involving the fixed statement fixing a string.
为以下对象生成特殊的编译器生成名称:
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
其中:
The pattern for generating magical names is: P<N>C__SI
where:
- 对于缓存的委托和显示类实例,P 是 CS$,否则为空.
- N 是与事物关联的原始名称(如果有)
- C 是上面列出的字符 1 到 s
- S 是描述性后缀(当前"、状态"等),因此您在读取元数据时不必记住上面的表格.
- I 是可选的唯一编号
这篇关于在哪里可以了解 VS 调试器的“魔术名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!