为什么编译器会自动为匿名类型生成一个 Debugger 属性?
Why does the compiler automatically generate a Debugger attribute for anonymous type?
当像这样编译一个简单的匿名类型代码时:
public class Test
{
public static void Main()
{
var my_anonymous = new { Name = "anonymous"};
}
}
IL 代码为每个生成的匿名类型方法都有一个 Debugger 属性。例如等于:
.method public hidebysig virtual instance bool
Equals(object 'value') cil managed
{
.custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = ( 01 00 00 00 )
... // rest of IL code
为什么编译器会自动生成呢?我使用 Microsoft (R) Visual C# 编译器 4.6.1055.0 版编译它,并且(如果这有任何相关性)没有使用 VS cmd。
注意:有 this 删除属性的答案(不可能),但我想知道 "Why?"。
这是 DebuggerHiddenAttribute - 它只是告诉 Visual Studio 调试器这是一个生成的类型,不要在它的代码中停止。 (因为代码不是用户写的——它是由编译器生成的。)
允许其他调试器遵循相同的行为 - 如果您阅读了文档,就会发现此属性没有附加语义,因此所有调试器(或虚拟机)都可以随意(或免费)以任何方式解释它忽略它)。
因为匿名类型实现是由编译器自动完成的,让调试器单步执行自动生成的代码没有意义。
- 因为生成的代码没有bug;这将是一个编译器错误,而不是特定于您的代码的错误。
- 因为即使存在错误,您也无能为力,因为一旦编译,class 就会被重写。
- 因为每次调试时单步执行无趣的代码很烦人。
来自MSDN
The common language runtime attaches no semantics to this attribute.
It is provided for use by source code debuggers. For example, the
Visual Studio 2005 debugger does not stop in a method marked with this
attribute and does not allow a breakpoint to be set in the method.
Other debugger attributes recognized by the Visual Studio 2005
debugger are the DebuggerNonUserCodeAttribute and the
DebuggerStepThroughAttribute.
我认为这是添加的,因为代码是在编译时生成的,而不是由程序员编写的,因此用户代码中不存在某些生成的代码行,调试器无法显示它。
当像这样编译一个简单的匿名类型代码时:
public class Test
{
public static void Main()
{
var my_anonymous = new { Name = "anonymous"};
}
}
IL 代码为每个生成的匿名类型方法都有一个 Debugger 属性。例如等于:
.method public hidebysig virtual instance bool
Equals(object 'value') cil managed
{
.custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = ( 01 00 00 00 )
... // rest of IL code
为什么编译器会自动生成呢?我使用 Microsoft (R) Visual C# 编译器 4.6.1055.0 版编译它,并且(如果这有任何相关性)没有使用 VS cmd。
注意:有 this 删除属性的答案(不可能),但我想知道 "Why?"。
这是 DebuggerHiddenAttribute - 它只是告诉 Visual Studio 调试器这是一个生成的类型,不要在它的代码中停止。 (因为代码不是用户写的——它是由编译器生成的。)
允许其他调试器遵循相同的行为 - 如果您阅读了文档,就会发现此属性没有附加语义,因此所有调试器(或虚拟机)都可以随意(或免费)以任何方式解释它忽略它)。
因为匿名类型实现是由编译器自动完成的,让调试器单步执行自动生成的代码没有意义。
- 因为生成的代码没有bug;这将是一个编译器错误,而不是特定于您的代码的错误。
- 因为即使存在错误,您也无能为力,因为一旦编译,class 就会被重写。
- 因为每次调试时单步执行无趣的代码很烦人。
来自MSDN
The common language runtime attaches no semantics to this attribute. It is provided for use by source code debuggers. For example, the Visual Studio 2005 debugger does not stop in a method marked with this attribute and does not allow a breakpoint to be set in the method. Other debugger attributes recognized by the Visual Studio 2005 debugger are the DebuggerNonUserCodeAttribute and the DebuggerStepThroughAttribute.
我认为这是添加的,因为代码是在编译时生成的,而不是由程序员编写的,因此用户代码中不存在某些生成的代码行,调试器无法显示它。