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.
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...
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.
You can use this to see if a member is defined: var expandoObject = ...; if(((IDictionary<String, object>)expandoObject). ContainsKey("SomeMember")) { // expandoObject. SomeMember exists. }
Why do you have the Enable Just My Code disabled in your VS options?
Now you will get an additional option in Exceptions dialog to break only on exceptions which you didn't handle in your code:
If you configure VS like this, the debugger won't break any more when these internally handled exceptions are thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With