Why am I getting a RuntimeBinderException using Json.NET?

The following C # calls Microsoft.CSharp.RuntimeBinder. RuntimeBinderException on line 2.

dynamic element = JsonConvert.DeserializeObject<dynamic>("{ Key: \"key1\" }");
bool match = "key1".Equals(element.Key, StringComparison.InvariantCulture);

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Member 'object.Equals (object, object)' cannot be accessed with reference to the instance; qualify it instead of type name

Link to the Json.NET 8.0.3 project

<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net452" />

I can get around the exception by explicitly converting element.Keyto System.String.

bool match = "key1".Equals((string)element.Key, StringComparison.InvariantCulture);

When checking element.Key.GetType()returns Newtonsoft.Json.Linq.JValue.

Why doesn't the DLR seem to know which method to call and end the call to the static method object.Equals(object, object)?

Edit:

, , , dynamic, System.Object .

bool match = "key1".Equals((object)element.Key, StringComparison.InvariantCulture);
+4
2

DLR, , , .Equals(object, object)?

element.Key string, JToken, .

, : static object.Equals(objA, objB), string.Equals(value, comparisonType), .

, :

dynamic foo = new { Foo = false };
bool equals = "Bar".Equals(foo.Foo, StringComparison.InvariantCulture);

.

+1

ExpandoObject?

dynamic element = new ExpandoObject();
        element = JsonConvert.DeserializeObject<ExpandoObject>("{ Key: \"key1\" }");            
        bool match = "key1".Equals(element.Key, StringComparison.InvariantCulture); 

: https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

0

All Articles