pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

處理動態時拋出很多第一次機會 Microsoft.CSharp.R

Lots of first chance Microsoft.CSharp.RuntimeBinderExceptions thrown when dealing with dynamics(處理動態時拋出很多第一次機會 Microsoft.CSharp.RuntimeBinderExceptions)
本文介紹了處理動態時拋出很多第一次機會 Microsoft.CSharp.RuntimeBinderExceptions的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 C# 中有一個標準的動態字典"類型類 -

class Bucket : DynamicObject{只讀字典<字符串,對象>m_dict = 新字典<字符串,對象>();public override bool TrySetMember(SetMemberBinder binder, object value){m_dict[binder.Name] = 值;返回真;}public override bool TryGetMember(GetMemberBinder binder, out object result){return m_dict.TryGetValue(binder.Name, out result);}}

現在我這樣稱呼它:

static void Main(string[] args){動態 d = new Bucket();d.Name = "獵戶座";//2個RuntimeBinderExceptionsConsole.WriteLine(d.Name);//2個RuntimeBinderExceptions}

該應用程序按照您的預期執行,但調試輸出如下所示:

<上一頁>Microsoft.CSharp.dll 中出現了Microsoft.CSharp.RuntimeBinder.RuntimeBinderException"類型的第一次機會異常Microsoft.CSharp.dll 中出現了Microsoft.CSharp.RuntimeBinder.RuntimeBinderException"類型的第一次機會異常ScratchConsoleApplication.vshost.exe"(托管(v4.0.30319)):已加載匿名托管的 DynamicMethods 程序集"Microsoft.CSharp.dll 中出現了Microsoft.CSharp.RuntimeBinder.RuntimeBinderException"類型的第一次機會異常Microsoft.CSharp.dll 中出現了Microsoft.CSharp.RuntimeBinder.RuntimeBinderException"類型的第一次機會異常

任何 訪問動態成員的嘗試似乎都會向調試日志輸出 RuntimeBinderException.雖然我知道第一次機會異常本身并不是問題,但這確實給我帶來了一些問題:

  1. 當我正在編寫 WPF 應用程序時,我經常將調試器設置為異常中斷",否則所有異常最終都會轉換為 DispatcherUnhandledException,而所有實際的你想要的信息丟失了.WPF 就是這么爛.

  2. 只要我遇到任何使用 dynamic 的代碼,調試輸出日志就會變得毫無用處.我關心的所有有用的跟蹤行都隱藏在所有無用的 RuntimeBinderExceptions

有什么辦法可以關閉它,或者不幸的是 RuntimeBinder 就是這樣構建的?

謝謝,獵戶座

解決方案

每當解析動態對象的屬性時,運行時都會嘗試查找在編譯時定義的屬性.來自 DynamicObject 文檔:

<塊引用>

您也可以將自己的成員添加到從 DynamicObject 派生的類班級.如果您的班級定義屬性,并覆蓋TrySetMember 方法,動態語言運行時 (DLR) 首先使用尋找靜態語言的活頁夾類中屬性的定義.如果沒有這樣的屬性,DLR調用 TrySetMember 方法.

RuntimeBinderException 在運行時找不到靜態定義的屬性時拋出(即在 100% 靜態類型的世界中會出現編譯器錯誤).來自 MSDN 文章

<塊引用>

...RuntimeBinderException 表示一個在某種意義上沒有約束力常見的編譯器錯誤...

有趣的是,如果您使用 ExpandoObject,您在嘗試使用該屬性時只會遇到一個異常:

動態bucket = new ExpandoObject();桶.SomeValue = 45;int value = bucket.SomeValue;//<-- 這里例外

也許 ExpandoObject 可以替代?如果不合適,您需要考慮實施 IDynamicMetaObjectProvider,這就是 ExpandoObject 進行動態調度的方式.但是,它的文檔記錄不是很好,MSDN 建議您參考 DLR CodePlex 了解更多信息.

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

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. 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.

  2. 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

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

Thanks, Orion

解決方案

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:

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 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 represents a failure to bind in the sense of a usual compiler error...

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

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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is there a C# library that will perform the Excel NORMINV function?(是否有執行 Excel NORMINV 函數的 C# 庫?)
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權列表中選擇 x 個隨機元素(無需替換))
Create a summary description of a schedule given a list of shifts(給定輪班列表,創建時間表的摘要描述)
C# Normal Random Number(C# 普通隨機數)
Standard deviation of generic list?(通用列表的標準偏差?)
AsyncCTP: Creating a class that is IAwaitable(AsyncCTP:創建一個 IAwaitable 的類)
主站蜘蛛池模板: 学习虾-免费的学习资料下载平台| 菲希尔X射线测厚仪-菲希尔库伦法测厚仪-无锡骏展仪器有限责任公司 | 岩棉切条机厂家_玻璃棉裁条机_水泥基保温板设备-廊坊鹏恒机械 | 电车线(用于供电给电车的输电线路)-百科 | IP检测-检测您的IP质量| 恒湿机_除湿加湿一体机_恒湿净化消毒一体机厂家-杭州英腾电器有限公司 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 光栅尺_Magnescale探规_磁栅尺_笔式位移传感器_苏州德美达 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 柴油发电机组_柴油发电机_发电机组价格-江苏凯晨电力设备有限公司 | 河北中仪伟创试验仪器有限公司是专业生产沥青,土工,水泥,混凝土等试验仪器的厂家,咨询电话:13373070969 | 断桥铝破碎机_铝合金破碎机_废铁金属破碎机-河南鑫世昌机械制造有限公司 | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 西装定制/做厂家/公司_西装订做/制价格/费用-北京圣达信西装 | CCC验厂-家用电器|服务器CCC认证咨询-奥测世纪 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 高尔夫球杆_高尔夫果岭_高尔夫用品-深圳市新高品体育用品有限公司 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 高柔性拖链电缆_卷筒电缆_耐磨耐折聚氨酯电缆-玖泰特种电缆 | 电缆接头-防爆电缆接头-格兰头-金属电缆接头-防爆填料函 | B2B网站_B2B免费发布信息网站_B2B企业贸易平台 - 企资网 | 美缝剂_美缝剂厂家_美缝剂加盟-地老板高端瓷砖美缝剂 | 14米地磅厂家价价格,150吨地磅厂家价格-百科 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 企业VI设计_LOGO设计公司_品牌商标设计_【北京美研】 | 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 应急灯_消防应急灯_应急照明灯_应急灯厂家-大成智慧官网 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 百度网站优化,关键词排名,SEO优化-搜索引擎营销推广 | 精密钢管,冷拔精密无缝钢管,精密钢管厂,精密钢管制造厂家,精密钢管生产厂家,山东精密钢管厂家 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-沼河浸过滤器 | 照相馆预约系统,微信公众号摄影门店系统,影楼管理软件-盟百网络 | 展厅设计-展馆设计-专业企业展厅展馆设计公司-昆明华文创意 | 螺旋压榨机-刮泥机-潜水搅拌机-电动泥斗-潜水推流器-南京格林兰环保设备有限公司 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 车载加油机品牌_ 柴油加油机厂家 | Magnescale探规,Magnescale磁栅尺,Magnescale传感器,Magnescale测厚仪,Mitutoyo光栅尺,笔式位移传感器-苏州连达精密量仪有限公司 | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 泰国试管婴儿_泰国第三代试管婴儿费用|成功率|医院—新生代海外医疗 |