Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting Constructor of a class from outisde

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 * * *

like image 472
awildturtok Avatar asked Dec 17 '22 20:12

awildturtok


2 Answers

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);
like image 84
Jeff Sternal Avatar answered Dec 19 '22 08:12

Jeff Sternal


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() { }
}
like image 36
Steve Danner Avatar answered Dec 19 '22 10:12

Steve Danner