1

I am a c# beginner (but quite familiar with JavaScript), and I am learning by debugging example code. I have a question now regarding the "immediate window".

I set a breakpoint at

  (... nested object sent via JSON from some external javascript code ...)
var json_serializer = new JavaScriptSerializer();
var value_list = (IDictionary<string, object>)json_serializer.DeserializeObject(value);

Then evaluated the following in the immediate window

value_list
Count = 4
    [0]: {[type, msg]}
    [1]: {[settings, System.Collections.Generic.Dictionary`2[System.String,System.Object]]}
    [2]: {[hello, edge]}
    [3]: {[txt, notepad.exe]}

value_list["txt"]
"notepad.exe"


value_list["settings"]
Count = 2
    [0]: {[host, test.com]}
    [1]: {[port, 80]}

So far so good.

Then I tried

value_list["settings"]["host"]

But only got "error CS0021: Cannot apply indexing with [] to an expression of type 'object'".

How can I print the value of host in the immediate window?

  • 3
    If you are just learning, ditch and forget that the class `JavaScriptSerializer` ever exister. Use the NewtonSoft.Json package, it has been the standard for a very long time – Camilo Terevinto Mar 01 '19 at 15:23
  • yes, JsonConvert is much better – Stormhashe Mar 01 '19 at 15:28
  • JavascriptSerializer is an obsolete class introduced way back when AJAX first came out. Every stack since ASP.NET Web API uses JSON.NET, aka NewtonSoft.Json, instead. Just don't use JavascriptSerializer – Panagiotis Kanavos Mar 01 '19 at 15:28
  • Noted (and I saw the same advice in Microsoft's API documentation already). –  Mar 01 '19 at 16:06

1 Answers1

1

C# is a strongly typed language.

You have

var value_list = (IDictionary<string, object>)json_serializer.DeserializeObject(value);

The var means that the type of the variable is determined by the compiler during compilation already. Because of the cast at the right side, the compiler determines this:

IDictionary<string, object> value_list = (IDictionary<string, object>)json_serializer.DeserializeObject(value);

So, value_list is of type IDictionary<string, object>. So, value_list["settings"] is of type object.

To see the value in the immediate window cast the intermediate result (which is of the type object) to the appropriate type.

((IDictionary<string, object>)value_list["settings"])["host"]
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • #1 works. Thank you! #2 does not work error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' Although a reference has been added already. But this should be separate question. I just wanted to point out that #1 seems to be the "easier" solution. –  Mar 01 '19 at 16:01
  • @cscharf Yeah, I have deleted the second proposal from the answer. Using `dynamic` in the immediate window only works in projects where `dynamic` is already used in the actual project source (which one should not do except for very specific situations). – NineBerry Mar 01 '19 at 16:05
  • Got it. Thanks. –  Mar 01 '19 at 16:11