I'm having troubles with deserializing an object with a dynamic field, which will hold different api responses. This is how it looks like:
public class ServiceCallResult
{
private dynamic _dataObject;
private Type _dataType;
public Type DataType
{
get
{
return _dataType;
}
set
{
_dataType = value;
}
}
public dynamic DataObject
{
get
{
return _dataObject;
}
set
{
_dataObject = value;
}
}
public string ErrorMessage { get; set; }
public bool Success { get; set; }
public ServiceCallResult()
{
}
public ServiceCallResult(Type type, dynamic obj)
{
DataType = type;
DataObject = obj;
Success = true;
}
}
And it could hold an object like this:
public class ApiPractitioner
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CompanyName { get; set; }
public int CVR { get; set; }
public string Address { get; set; }
public short ZipCode { get; set; }
public string City { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Description { get; set; }
public byte[] ProfileImage{ get; set; }
//[JsonIgnore]
//public List<ApiPractice> Practices { get; set; }
//[JsonIgnore]
//public List<ApiClient> Clients { get; set; }
}
But when deserializing with this code:
var req = new RestRequest("practitioner/createpractitioner", Method.POST);
req.RequestFormat = DataFormat.Json;
req.AddBody(prac);
var response = _client.Execute<ServiceCallResult>(req);
ServiceCallResult td = new JsonDeserializer().Deserialize<ServiceCallResult>(response);
It ends up with a "MissingMethodException" in mscorlib.dll, additional information is "An abstract class can't be created". The response object has the json content but there's and exception like the one when I use the JsonDeserializer and I can't figure out what's going on, I've searched for hours on Google and tried different suggestions with no luck. Anyone tried the same with a dynamic property?
I'm using RestSharp for the json handling.
Best regards Benjamin
After a little enlightment from Michael I've changed my ServiceCallResult to this:
public class ServiceCallResult<T> where T : BaseApiResponse
{
public T DataObject { get; set; }
public string ErrorMessage { get; set; }
public bool Success { get; set; }
public ServiceCallResult()
{
}
public ServiceCallResult(T obj)
{
DataObject = obj;
Success = true;
}
}
And all my api responses inherits from BaseApiResponse.