Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why 'dynamic' ExpandoObject throws RuntimeBinderException even if it contains the definition for a property?

Tags:

c#

.net

exception

Using the following sample code: (VS 2013, update 3)

dynamic demo = new ExpandoObject();
demo.Test = 10;
var j = demo.Test; // throws exception

When debugging this code and 'Break when an exception is: 'Thrown'' is checked in VS then trying to access the existing property 'Test' throws a RuntimeBinderException:

System.Dynamic.ExpandoObject' does not contain a definition for 'Test'

Note: Stepping over to the next line the variable j is set correctly to 10.

enter image description here


enter image description here


Q1: Why the exception is thrown, when the Test property definitely exists and holds the value 10 which is proved by the fact variable j successfully set this value? The exactly same exception occurs when I try to use a really non existing property name like Test2, with the difference that then the code really jumps out the block with a non handled exception...

Throwing a totally false and misleading exception, then handle internally it just does not make sense, besides it renders VS 'Break when an exception is: 'Thrown' option feature practically unusable for code what uses dynamic objects.

Q2: Yes I know this issue can be hidden by unchecking 'Break when an exception is: 'Thrown' option. However this is not an option supposing a developer uses dynamic objects, and try to find exceptions in her/his code what are totally unrelated with the dynamic objects, this issue make the 'Break when an exception is: 'Thrown' option unusable, because then the debugger will stop thousands of correct property access statement. Is there any workaround?

Missed I something?

Thanks in advance.


* Edit * This edit is after the correct answer.

Damir asked in his answer "Why do I have the Enable Just My Code disabled in your VS options?" Well here is the answer... I was bugged with this for a few days, and experienced that despite I explicitly check the Just My Code option, it somehow does not persist between the VS close/start sessions. Finally I found out why...

enter image description here

like image 998
g.pickardou Avatar asked Dec 02 '14 11:12

g.pickardou


People also ask

What is System Dynamic expandoObject?

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax like sampleObject. sampleMember instead of more complex syntax like sampleObject.

How do I know if I have expandoObject property?

You can use this to see if a member is defined: var expandoObject = ...; if(((IDictionary<String, object>)expandoObject). ContainsKey("SomeMember")) { // expandoObject. SomeMember exists. }


1 Answers

Why do you have the Enable Just My Code disabled in your VS options?

enter image description here

Now you will get an additional option in Exceptions dialog to break only on exceptions which you didn't handle in your code:

enter image description here

If you configure VS like this, the debugger won't break any more when these internally handled exceptions are thrown.

like image 85
Damir Arh Avatar answered Oct 24 '22 14:10

Damir Arh