与动力打交道时抛出的第一个机会Microsoft.CSharp.RuntimeBinderExceptions大量的 [英] Lots of first chance Microsoft.CSharp.RuntimeBinderExceptions thrown when dealing with dynamics

查看:531
本文介绍了与动力打交道时抛出的第一个机会Microsoft.CSharp.RuntimeBinderExceptions大量的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标准的动态字典类型的类在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);
    }
}

现在我把它称为,如下所示:

Now I call it, as follows:

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. 我经常调试器设置为on异常中断,因为我正在写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.

只要我打的是一个使用动态,调试输出日志变得相当无用的code。所有这一切我在乎得到隐藏跻身有用的跟踪线路中的全部没用 RuntimeBinderException 取值

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

有没有什么办法可以关闭此功能,或者是 RuntimeBinder 不幸的是刚刚建成这样?

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

谢谢,猎户座

推荐答案

每当一个动态对象上的属性得到解决,运行时尝试发现,在编译时定义的一个属性。从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:

您也可以自己添加成员
  从DynamicObject派生类
  类。如果你的类定义
  性能也覆盖了
  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.

RuntimeBinderException 被抛出(即。这将是100%的静态类型的世界编译器错误)。从<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.csharp.runtimebinder.runtimebinderexception.aspx\">MSDN文章

RuntimeBinderException is thrown whenever the runtime cannot find a statically defined property(i.e. what would be a compiler error in 100% statically typed world). From MSDN article

... RuntimeBinderException重新presents一个
  未能在一个意义上结合
  通常的编译器错误...

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

有趣的是,如果你使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx\"><$c$c>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 可能是一个选择吗?如果它不适合你需要考虑实施<一个href=\"http://msdn.microsoft.com/en-us/library/system.dynamic.idynamicmetaobjectprovider.aspx\"><$c$c>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.RuntimeBinderExceptions大量的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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