这是因为Expression.ToString
覆盖依赖于内部ExpressionStringBuilder
访问者类型,它产生一个大大简化的表达式树表示。
每个上定义的自定义调试器代理提供的调试视图Expression
-派生类型(即[DebuggerTypeProxy(typeof(Expression.BlockExpressionProxy))]
on BlockExpression
)正如您所发现的,通过公开更详细的输出,提供了更多信息DebugViewWriter
访客(也是内部的)。
不幸的是,除非您愿意使用反射来获取私有的值,否则您无法在调试场景之外轻松获得该输出DebugView
属性(定义于System.Linq.Expressions.Expression
) 如下:
Expression<Func<string, int>> expr = str => str.Length;
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
PropertyInfo debugViewProp = typeof(Expression).GetProperty("DebugView", flags);
MethodInfo debugViewGetter = debugViewProp.GetGetMethod(nonPublic: true);
string debugView = (string)debugViewGetter.Invoke(expr, null);
Produces
.Lambda #Lambda1<System.Func`2[System.String,System.Int32]>(System.String $str) {
$str.Length
}
一如既往,参考源是您最好的朋友:
http://referencesource.microsoft.com/#System.Core/Microsoft/Scripting/Ast/Expression.cs,aa5f054356a8a17d http://referencesource.microsoft.com/#System.Core/Microsoft/Scripting/Ast/Expression.cs,aa5f054356a8a17d