So, the question is simple to aks: How can I overwrite the constructor of a class from the outside. The Problem itself is, that i have a class which is already compiled, and it already has some constructors, but those idiots of coders removed a constructor, so i am now unable to XML(de)Serialize it...
So what they have done is this:
They changed
Vector2();
Vector2( x, y);
into
Vector2(x=0,y=0);
But my Problem is, that the Serializer isn't that intelligent to realize that he can still create the class, and changing the entire code would be a pain in the * * *
Inherit from it and provide the expected constructor yourself.
You can use deserialized instances of the derived class where your code expects Vector2
instances:
public class Vector3: Vector2 {
public Vector3(): base(0, 0) {}
}
// Your code:
Vector2 vector = (Vector3)XmlSerializer.Deserialize(xmlReader);
If by some chance the class was marked as partial, you can add it with your own partial class declaration:
public partial class CompiledClass
{
public CompiledClass() { }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With