[英]How to debug Expression.Lambda? (Difference in Expressions tree evaluation between .net core 2.1 and .net core 3.1)
I've created a small program that shows some differences between .net core 2.1 and 3.1:我创建了一个小程序,它显示了 .net 核心 2.1 和 3.1 之间的一些差异:
using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;
public class Program
{
public static void Main()
{
var entityObject = Expression.Parameter(typeof(object), "objParam");
var cev = new ExpressionPrinter();
var entObjPrint = cev.PrintDebug(entityObject);
Console.WriteLine(entObjPrint);
}
}
the print out in 2.1: Unhandled parameter: objParam 2.1中的打印输出:Unhandled parameter: objParam
the print out in 3.1: (Unhandled parameter: objParam){0} 3.1 中的打印输出:(未处理的参数:objParam){0}
notice the brackets and the extra {0} at the end.请注意末尾的方括号和额外的 {0}。 Can someone explain to me why is this difference?有人可以向我解释为什么会有这种差异吗?
NOTE: This is a shortened version of my issue, but also when I try to compile an Expression.Lambda in .net core 3.1 I get an error (not exception) which is not present in the .net core 2.1 version of my code:注意:这是我的问题的简化版本,但当我尝试在 .net 核心 3.1 中编译 Expression.Lambda 时,我收到一个错误(不是异常),该错误在我的代码的 881812122262088 核心 2.1 版本中不存在:
{Method = <Internal Error evaluating expression>}
UPDATE : It seems that the PrintDebug
implementation have changed.更新:似乎PrintDebug
实现已更改。 If you call PrintDebug(enitytObject,null,false)
there will no longer be {0}.如果您调用PrintDebug(enitytObject,null,false)
,将不再有 {0}。
With that in mind, maybe the issue is not that, but still I do not know how to debug a compile error of an lambda expression (not to be mistaken with linq lambda).考虑到这一点,也许问题不在于此,但我仍然不知道如何调试 lambda 表达式的编译错误(不要与 linq lambda 混淆)。
More on the issue This is part of the code where I am getting the error:有关此问题的更多信息这是我收到错误的代码的一部分:
//return all fields/properties that have an attribute indicating they need to be localized
var locFields = EntityLocalizationReflection.GetLocalizationFields(entityType);
if (locFields?.Count > 0)
{
var updater = new EntityLocalizerUpdater();
// create a parameter representing an object (object)
var entityObject = Expression.Parameter(typeof(object), "objParam");
// convert the parameter to an IEntityType ((object)Category)
var entityInstance = Expression.Convert(entityObject, entityType.Metadata.ClrType);
// iterate over all fields/properties that needs to be localized
foreach (var loc in locFields)
{
// Depending if the localized element is a field or a property, create the expression
var target = loc.Item2.MemberType == MemberTypes.Property
? Expression.Property(entityInstance, (PropertyInfo)loc.Item2)
: Expression.Field(entityInstance, (FieldInfo)loc.Item2);
var getter = Expression.Lambda<Func<object, EntityLocalizer>>(target, entityObject);false);
//getter.Compile() failes only in .NET code 3.1
updater.Add(getter.Compile(), loc.Item1.EntityName);
}
//...some other code..//
}
UPDATE 2 : Thanks to @MarcGravell there is a minimum runnable code here: https://gist.github.com/mgravell/57d5691741e1379f32c2d22540acf8da and the error can be viewed if a "Quick Watch" is performed over getter.Compile()
or assign some variable to it ( var compiled = getter.Compile();
) and then inspect it during Debug.更新 2 :感谢@MarcGravell,这里有一个最小可运行代码: https://gist.github.com/mgravell/57d5691741e1379f32c2d22540acf8da如果通过getter.Compile()
执行“快速观察”或分配,则可以查看错误它的一些变量( var compiled = getter.Compile();
)然后在调试期间检查它。
It seems that it is a debugger issue when showing Expressions in .net core 3.1.在 .net 核心 3.1 中显示表达式时,似乎是调试器问题。 An issue has been open.一个问题已经打开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.