Reputation: 35973
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...
Upvotes: 15
Views: 7449
Reputation: 317
I encountered this issue recently and It was because I am trying to add an object to a list which is a property of the ExpandoObject without initializing the list first.
var someObjectInstance = new someObject();
var sampleExpando = new ExpandoObject();
sampleExpando.someObjects.Add(someObjectInstance);
The above gave me RuntimeBinding Error which was resolved by the following:
var someObjectInstance = new someObject();
var sampleExpando = new ExpandoObject();
sampleExpando.someObjects = new List<someObject>();
sampleExpando.someObjects.Add(someObjectInstance);
Upvotes: -1
Reputation: 302
I solved this by using the comment made by "Jeppe Stig Nielsen". Just uncheck the Break when this exception type is thrown, and the debugger will no longer prevent the code from running.
Upvotes: 1
Reputation: 17865
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.
Upvotes: 10