awildturtok
awildturtok

Reputation: 173

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

Upvotes: 4

Views: 385

Answers (2)

Jeff Sternal
Jeff Sternal

Reputation: 48673

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);

Upvotes: 9

Steve Danner
Steve Danner

Reputation: 22188

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() { }
}

Upvotes: 3

Related Questions