c# - How to serialize derived class to base class? -
i want serialize object , pass method parameter type parent of object.
for example, have classes.
public class base { public string typename => gettype().name; public string data => jsonconvert.serializeobject(this); } public class derived : base { public string name { get; set; } public int data1 { get; set; } public int data2 { get; set; } } public class derived2 : base { ... } ....
i wrote code follows,
var obj = new derived { name = "john", data1 = 2000, data2 = 1500 }; send(obj);
and send(..) method is,
public void send(base info) { // "info". }
when instantiate variable obj, program has fallen infinite recursion because of "data" in base class.
how can change code?
infinite recursion caused data property, serialized - causes serialization of this , loop begins.
the best solution change property method, not serialized , better serve purpose. if dead set on property - try marking property http://www.newtonsoft.com/json/help/html/propertyjsonignore.htm cause ignored during serialization.
Comments
Post a Comment