Why is a JValue held in a "dynamic" variable, assignable to a string variable, but not usable when calling a method taking a string parameter?

This comes from this question really: Looping through JSON array and adding items to list C# .

To not derail that question completely, I ask a new one that I started to wonder about.

Using Json.net, why does this code work:

dynamic test = new JValue("test");
string s = test;

while this:

dynamic test = new JValue("test");
var list = new List<string>();


RuntimeBinderException: The best overloaded method match for 'System.Collections.Generic.List<string>.Add(string)' has some invalid arguments

Given that there is just one overload for the Add method, and it takes a string, why won't it silently do the runtime conversion here as well?

Read more here: https://stackoverflow.com/questions/64484213/why-is-a-jvalue-held-in-a-dynamic-variable-assignable-to-a-string-variable-b

Content Attribution

This content was originally published by Lasse V. Karlsen at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: