We have a framework built up whose API is this:
T Deserialize<T>(string to deserialize)
{
// XmlSerializer(typeof(T))
// deserialize and return
}
We’ve been passing it things like:
[XmlRoot("apple")]
public class Apple
{
[XmlElement("Id")]
public int AppleId { get; set; }
}
this works great when we are returned an apple, however, sometimes we are getting “notAnApple”. So I will either get “apple” or “notAnApple”. When I get “notAnApple”, I get an exception.
As we have a lot of code built around this method, I would prefer not to go around changing things like the method signature. I do, however, have full flexibility into passing whatever class I like into this deserialization method.
Generally you “know” exactly what you’re serializing and deserializing. e.g. you may serialize like this:
Then you’d serialize like:
You don’t normally randomly try to deserialize data to figure out what it is that you’re deserializing. Technically you already know because you have a specific file format or a specific stream format.