很多第一次机会Microsoft.CSharp.RuntimeBinderException在处理动态时抛出 [英] Lots of first chance Microsoft.CSharp.RuntimeBinderExceptions thrown when dealing with dynamics

查看:1006
本文介绍了很多第一次机会Microsoft.CSharp.RuntimeBinderException在处理动态时抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个标准的'动态字典'类型类 -

I've got a standard 'dynamic dictionary' type class in C# -

class Bucket : DynamicObject
{
    readonly Dictionary<string, object> m_dict = new Dictionary<string, object>();

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        m_dict[binder.Name] = value;
        return true;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return m_dict.TryGetValue(binder.Name, out result);
    }
}

现在我称之为如下:

static void Main(string[] args)
{
    dynamic d = new Bucket();
    d.Name = "Orion"; // 2 RuntimeBinderExceptions
    Console.WriteLine(d.Name); // 2 RuntimeBinderExceptions
}

应用程序执行你所期望的,但是调试输出如下所示:

The app does what you'd expect it to, but the debug output looks like this:


A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
'ScratchConsoleApplication.vshost.exe' (Managed (v4.0.30319)): Loaded 'Anonymously Hosted DynamicMethods Assembly'
A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll

尝试访问动态成员的任何似乎输出一个 RuntimeBinderException 到调试日志。虽然我知道第一例异常本身并不是问题,但这确实给我带来了一些问题:

Any attempt to access a dynamic member seems to output a RuntimeBinderException to the debug logs. While I'm aware that first-chance exceptions are not a problem in and of themselves, this does cause some problems for me:


  1. 我经常将调试器设置为打破异常,因为我正在编写WPF应用程序,否则所有异常都会转换为 DispatcherUnhandledException ,并且所有你想要的实际信息会丢失。 WPF很像这样。

  1. I often have the debugger set to "break on exceptions", as I'm writing WPF apps, and otherwise all exceptions end up getting converted to a DispatcherUnhandledException, and all the actual information you want is lost. WPF sucks like that.

一旦我打了任何使用动态的代码,调试输出日志变得相当无用。我所关心的所有有用的跟踪行隐藏在所有无用的中RuntimeBinderException s

As soon as I hit any code that's using dynamic, the debug output log becomes fairly useless. All the useful trace lines that I care about get hidden amongst all the useless RuntimeBinderExceptions

有没有什么办法可以关闭这个,或者是$ code> RuntimeBinder 不幸的是建立了这样的一个?

Is there any way I can turn this off, or is the RuntimeBinder unfortunately just built like that?

感谢Orion

推荐答案

每当动态对象的属性被解析时,运行时会尝试查找属性这是在编译时定义的。从DynamicObject doco:

Whenever a property on a dynamic object is resolved, the runtime tries to find a property that is defined at compile time. From DynamicObject doco:


您还可以将自己的成员添加到从$动态对象
类派生的
类中。如果您的类定义
属性并且还覆盖
TrySetMember方法,则动态
语言运行时(DLR)首先使用
语言绑定来查找静态
定义在课堂上的财产。
如果没有这样的属性,DLR
调用TrySetMember方法。

You can also add your own members to classes derived from the DynamicObject class. If your class defines properties and also overrides the TrySetMember method, the dynamic language runtime (DLR) first uses the language binder to look for a static definition of a property in the class. If there is no such property, the DLR calls the TrySetMember method.

。从 MSDN文章


... RuntimeBinderException表示
无法绑定在
通常的编译器错误...

...RuntimeBinderException represents a failure to bind in the sense of a usual compiler error...

有趣的是,如果您使用 ExpandoObject ,您只能在尝试使用该属性时收到一个例外:

It is interesting that if you use ExpandoObject, you only get one exception when trying to use the property:

dynamic bucket = new ExpandoObject();
bucket.SomeValue = 45;
int value = bucket.SomeValue; //<-- Exception here

也许 ExpandoObject 可能是一种替代方案?如果不合适,您需要考虑实施 IDynamicMetaObjectProvider ,这是如何 ExpandoObject 进行动态调度。但是,它不是很好的记录,MSDN指向您的DLR CodePlex了解更多信息。

Perhaps ExpandoObject could be an alternative? If it's not suitable you'll need to look into implementing IDynamicMetaObjectProvider, which is how ExpandoObject does dynamic dispatch. However, it is not very well documented and MSDN refers you to the DLR CodePlex for more info.

这篇关于很多第一次机会Microsoft.CSharp.RuntimeBinderException在处理动态时抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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