APW
APW

Reputation: 537

How to ignore an Obsolete/Deleted class during BSON deserialization

We have a class containing a list of types that all inherit the same interface. We have now made one of the inherited classes Obsolete and want to remove it from the code, but when we test this scenario, the app throws a deserialization error because it can't find the derived class to map the existing data to.

In the below example, we have a database of parents and many of those parents have a Children collection which includes a Fetus object. Our goal is to be able to make the Fetus Obsolete and then remove the class completely. Ideally, we would remove the code and at a later point in time clean up the database collection, but due to the BSON class mapping in C# this seems to not be possible.

public class Parent {
    public string Name { get; set; }
    public IChild[] Children { get; set; }
}

public class Son : IChild {
    public string Name { get; set; }
    public string Gender => "Male";
}

public class Daughter : IChild {
    public string Name { get; set; }
    public string Gender => "Female";
}

[Obsolete]
public class Fetus : IChild {
    public string Name { get; set; }
    public string Gender => "Fetus";
}

Upvotes: 0

Views: 23

Answers (0)

Related Questions