Structure
Class B extends Class A.
A implements Interface ISerializable
The ISerializabledefines a constructor:
public A(SerializationInfo info, StreamingContext ctxt)
I need to write a specifc implementation of this constructor in B.
I have tried simply putting the constructor in B - but it will not be called.
I can't seem to override it either.
The simplified problem
So A(SerializationInfo info, StreamingContext ctxt) always gets called instead of B(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt).
or
new Base() does not call new Derived()
The code that calls the (wrong) constructor:
UPDATE
- The objects are treated as objects of A - which might be the problem!
UPDATE
List<A> list = new List<A>();
list.Add(New B());
string s = JsonConvert.SerializeObject(list);
JsonConvert.DeserializeObject<List<A>>(s); <--- //it is called from here.
Any ideas to solve this inheritance problem?
Details
public class A: ISerializable
{
public A(int id, string name, string type, string category, string description, string data)
{
this.Id = id;
this.Name = name;
this.Type = type;
this.Category = category;
this.Description = description;
this.Data = data;
}
protected A(SerializationInfo info, StreamingContext ctxt)
{
Id = (int)info.GetValue("id", typeof(int));
Name = (String)info.GetValue("name", typeof(string));
Type = (String)info.GetValue("type", typeof(string));
Category = (String)info.GetValue("category", typeof(string));
Description = (String)info.GetValue("description", typeof(string));
Data = (String)info.GetValue("data", typeof(string));
}
}
public class B : A
{
public B(int id, string name, string type, string category, string description, string data) : base(id, name, type, category, description, data)
{
// specific B code
}
protected B(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt){
// THIS NEVER GETS CALLED
// specific B code
}
}