Reputation: 5691
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; }
}
Upvotes: 0
Views: 1374
Reputation: 13970
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.
Upvotes: 1
Reputation: 2904
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);
Upvotes: 0