Many of the first chances of Microsoft.CSharp.RuntimeBinderException that occur when working with dynamics

I have a standard class like "dynamic dictionary" 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 like this:

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

The application does what you expect from it, but the debug output is as follows:

 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 RuntimeBinderException in the debug logs. Although I know that exceptions from the first chance are not a problem in themselves, this causes some problems for me:

  • I often have a debugger set to "break on exceptions" because I write WPF applications, otherwise all exceptions end up being converted to DispatcherUnhandledException , and all the actual information you want to lose, WPF sucks.

  • As soon as I click on any code that uses dynamic , the debug output log becomes useless. All useful trace lines that interest me are hidden among all useless RuntimeBinderException s

Is it possible to disable this, or RuntimeBinder , unfortunately, just built?

Thanks, Orion

+47
c # dynamic exception
Jun 02 '10 at 1:55
source share
2 answers

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

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

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

... RuntimeBinderException represents an inability to communicate in the sense of a common compiler error ...

Interestingly, if you use ExpandoObject , you get only one exception when trying to use the property:

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

Maybe ExpandoObject could be an alternative? If this does not fit, you will need to study IDynamicMetaObjectProvider , namely how ExpandoObject performs dynamic dispatch. However, this is not well documented, and MSDN refers to the CodeRlex DLR for more information.

+23
Jun 02 '10 at 2:31
source share

It bothered me too. I added an exception to the exception list to discard it. Just follow these steps:

  • From the Debug menu, select Exceptions.
  • Click the "Add ..." button in the lower right corner.
  • Select "General runtime exceptions" in the "Type" drop-down list.
  • Enter "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException" as the name.
  • Click OK.
  • A list of exceptions appears in the list. Just unselect.

I would like this parameter to be saved in different solutions, but I do not think it is possible, so you will have to reuse this setting for each solution.

+20
Jun 02 2018-11-11T00:
source share



All Articles