0

I am trying to convert dynamic object contains JSON data into custom c# object and get the following error:

RuntimeBinderException The best overloaded method match for Newtonsoft.Json.JsonConvert.DeserializeObject(string, System.Type, params Newtonsoft.Json.JsonConverter[])' has some invalid arguments

The variable named communication is a dynamic object contains the following value (JSON data):

{
 "name": "inGame",
 "selected": true,
 "image": "assets/img/communication/ingame.png"
}

here is the code that should convert the dynamic into a custom c# object:

InGameCommunication inherited = JsonConvert.DeserializeObject(communication, typeof(InGameCommunication),
 new JsonSerializerSettings());

The class hierarchy:

public abstract class Communication
{
 public int Id { get; set; }
 public string Name { get; set; }
 public bool Selected { get; set; }
}
public class InGameCommunication : Communication
 {
 }
public class SkypeCommunication : Communication
{
 public string Username { get; set; }
}
asked Oct 4, 2013 at 21:23
2
  • But what is communication at runtime? Commented Oct 4, 2013 at 21:56
  • Have another look in my question, I've wrote its runtime value. Commented Oct 4, 2013 at 22:11

2 Answers 2

1

You've stated that communication is a dynamic object. However, that does not absolve you of type-safety. At run-time communication still needs to be a string (As stated in the error message).

You're bypassing compiler error by making the variable dynamic but at run-time if the variable isn't a string or of a conversion that can be inferred it will still throw.

See the msdn reference, specifically the heading on overload resolution.

answered Oct 4, 2013 at 22:19

Comments

0

That code looks like it should work. Can you show how you declared the variable Type?

It should be something like

var type = typeof(InGameCommunication);
answered Oct 4, 2013 at 21:29

1 Comment

Yes, its the same way I've declared my type variable. I've updated my post with this value.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.