3

There are many answers here which suggest the following ways to use JObject as dynamic.

dynamic dynObj = JObject.Parse(jsonString);
OR
dynamic dynObj = JsonConvert.DeserializeObject<dynamic>(jsonString);

My jsonString is simple: {"id":"123","name":"abc"}.

It does not seem to work in Newtonsoft.Json 9.0. When I try any of these, I still get an object with the Type object {Newtonsoft.Json.Linq.JObject}.

And when I try to access a property by doing dynObj.id I get an exception error CS1061: 'object' does not contain a definition for 'id' and no extension method 'id' accepting a first argument of type 'object' could be found.

eadam
  • 23,151
  • 18
  • 48
  • 71
  • 3
    Show your `jsonString` – haim770 Feb 15 '17 at 14:16
  • just added the jsonString – eadam Feb 15 '17 at 14:24
  • 1
    Do you have a [MCVE](http://stackoverflow.com/help/mcve) - I just tried this and it appears to work as expected (v9.0.1)? – petelids Feb 15 '17 at 14:28
  • I think I will try to create an example outside of my project. – eadam Feb 15 '17 at 14:35
  • Cannot reproduce, see https://dotnetfiddle.net/aXmSUv. – dbc Feb 15 '17 at 14:38
  • @dbc thanks for the fiddle. I believe, its something related to my project only then. Trying to use it in .net core 1.1. Thanks for the help guys. I will close this question for now and re-open with my findings. Don't want to waste time of other people. – eadam Feb 15 '17 at 14:41

1 Answers1

8

You are probably seeing the first chance exception being thrown in the debugger when the DLR first tries to bind to a property on the object. This exception can be safely ignored and if you continue running the code it should work fine.

see: Lots of first chance Microsoft.CSharp.RuntimeBinderExceptions thrown when dealing with dynamics

Brian Flynn
  • 269
  • 6
  • 13