I recently solved this error:
Type is an interface or abstract class and cannot be instantiated
,
by using the solution suggested here:
Using Json.NET converters to deserialize properties.
Unfortunately I am getting new issues with this. In the code below, Depth gets called as part of the deserialisation, which in turn calls the Prof property which fails because 'this.Profile' is null. ('this.Profile' is a property inherited from the Section class.)
public class TimberSection : Section, IPurlinShape, IEavesBeamShape
{
private Profiles.Flat Prof
{
get
{
var prof = this.Profile as Profiles.Flat;
if (prof != null)
return prof;
else
throw new DataMissingException("Expected Rectangle profile");
}
}
public override double Depth { get { return Prof.Depth; } }
}
I used http://www.jsoneditoronline.org/ to view my object and it seems everything is correct. The profile object that is coming up null when i deserialise, has a value the same as the object prior to serialization.
Below is my method for serialising it, then deserialising it immediately after.
string serialisedEnquiry = JsonConvert.SerializeObject(enquiry, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
});
Enquiry enq = JsonConvert.DeserializeObject<Enquiry>(serialisedEnquiry, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
});
What am i doing wrong here?