我创建了一个小程序,该程序显示了.net core 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);
}
}
在2.1中打印输出:未处理的参数:objParam
在3.1中打印输出:(未处理的参数:objParam){0}
请注意方括号和末尾的额外{0}。
有人可以向我解释为什么有这种区别吗?
注意:这是我的问题的简化版本,但是当我尝试在.net core 3.1中编译Expression.Lambda时,我得到一个错误(不是异常),该错误不在我的代码的.net core 2.1版本中:
{Method = <Internal Error evaluating expression>}
UPDATE :似乎PrintDebug
实现已更改。如果您调用PrintDebug(enitytObject,null,false)
,将不再是{0}。
考虑到这一点,也许问题不在于此,但我仍然不知道如何调试lambda表达式的编译错误(不要误认为linq lambda)。
有关此问题的更多信息,这是我收到错误的代码的一部分:
//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..//
}
更新2:感谢@MarcGravell,这里有一个最低限度的可运行代码:https://gist.github.com/mgravell/57d5691741e1379f32c2d22540acf8da,如果对getter.Compile()
执行“快速监视”或为其分配了一些变量(var compiled = getter.Compile();
),然后在调试过程中对其进行了检查,则可以查看该错误。
参考方案
在.net core 3.1中显示Expressions时,似乎是调试器问题。
一个问题已经解决。
在ASP.NET Core依赖注入中,我只是想知道注册Singleton实例是否会比注册Transient实例更好地提高性能?在我看来,对于Singleton实例,创建新对象和相关对象只需花费一次时间。对于Transient实例,此成本将针对每个服务请求重复。因此Singleton似乎更好。但是,在Singleton上使用Transient时,我们可以获得多…
如何在ASP.NET Core Web应用程序中增加JSON反序列化MaxDepth限制 - c#我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…
ASP.NET Core-在Singleton注入上存储库依赖项注入失败 - c#我正在使用SoapCore为我的ASP.NET Core MVC应用程序创建Web服务。我正在使用Entity Framework Core和简单的存储库模式来获取我的数据库数据。我通过Startup.cs中的.AddSingleton()注入存储库类:services.AddSingleton<IImportRepository, ImportRep…
为什么.NET Core API中的POST参数总是为null - c#我创建了一个简单的.NET核心控制器,并尝试从Angular查询它。问题在于,每当我向登录操作发送发布请求时,我总是会收到空参数!不过,很奇怪的是,在进行调查时,请求正文/有效负载清楚地显示了我的参数已设置。有关我的意思的示例,请参见我的屏幕截图。我已经尽了最大的努力,并且在过去的几个小时中一直在阅读类似的问题,但是没有取得任何成功?所以我的问题是:是什么导…
如何在ASP.NET Core(使用JavascriptService)应用程序中使Node.js代码调用csharp代码? - c#我正在将asp.net内核用于简单的Web api服务器(实际上是使用Deepstream)。虽然C#可以使用NodeServices.InvokeExportAsync完美地调用nodejs代码,但是当我尝试将Action / Func作为NodeServices.InvokeExportAsync的参数传递给nodejs时,却得到了System.Aggr…