使用.NET Native工具链进行构建会导致动态对象中缺少属性的错误 [英] Building with .NET Native tool chain causes error with missing property in dynamic object

查看:92
本文介绍了使用.NET Native工具链进行构建会导致动态对象中缺少属性的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码可以获取JSON响应并检查是否存在.error字段

I have a piece of code that gets a JSON response and checks whether there is a .error field

dynamic jsonResponse = JsonConvert.DeserializeObject(responseString);
if (jsonResponse.error != null) { error = jsonResponse.error; }
else
{
  success = true;
}

如果未使用.NET Native工具链进行编译,则此方法可以成功运行,但是使用它构建时会产生错误(在jsonResponse.error上).

This runs successfully when it is not compiled with .NET Native toolchain but produces an error (on jsonResponse.error) when it's built with it.

这是什么原因?还有其他与本机代码类似的不兼容行为吗?

What is the reason for this? Any other similar incompatible behavior with native code?

事实证明,即使JSON中有错误"键,我们仍然会收到错误消息.例外是:

It turns out that even if there is an "error" key in the JSON, we still get an error. The exception is:

System.Reflection.MissingMetadataException: ''Microsoft.CSharp.RuntimeBinder.CSharpGetMemberBinder' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859'

推荐答案

为dynamic关键字提供动力的子系统存在各种极端情况,这些情况在.NET Native上无法很好地运行.此特定问题已于2月报告给我们,您可以在CoreFX GitHub 此处 a>.

The subsystem that powers the dynamic keyword has various corner cases that don't run super well on .NET Native. This particular issue was reported to us in February, you can see some discussion on the CoreFX GitHub here.

通常的想法是,dynamic关键字会导致大量的机制在API中徘徊,并且框架的某些部分没有适当的提示说这不是您需要考虑的事情".因为我们的编译器分析说您在运行时不需要这种类型,但是此特定组件需要,所以我们最终抛出此异常.

The general idea is that the dynamic keyword causes a great deal of machinery to wander through APIs and some pieces of the framework don't have the proper hinting to say "This isn't a thing you need to reflect on." Because our compiler analysis says you won't need this type at runtime but this particular component does, we end up throwing this exception.

异常中的链接试图帮助构建运行时指令(将其视为对.NET Native编译器的提示),以便我们知道您在运行时需要类型信息.对于这种特殊情况,它看起来像:

The link in the exception is trying to help build a runtime directive (think of it as a hint to the .NET Native compiler) so that we'll know you'll need type information at runtime. For this particular case it would look like:

<Type Name="Microsoft.CSharp.RuntimeBinder.CSharpGetMemberBinder" Dynamic="Required All"/>

如果将其添加到文件Properties \ Default.rd.xml中,我希望此错误消失.您可能会遇到这种类型的其他错误,但是应该可以通过类似的方式来解决它们.

If you add that to the the file Properties\Default.rd.xml I would expect this error to disappear. You may hit additional errors of this type but they should be able to be addressed in a similar fashion.

我们已经记录了一个错误,以便将来在某个时候解决此问题,但是与此同时,您将需要此解决方法.

We've logged a bug on our side to get this addressed at some point in the future but you'll need this workaround in the meantime.

这篇关于使用.NET Native工具链进行构建会导致动态对象中缺少属性的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆