views:

113

answers:

2

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

+8  A: 

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);
Jeff Sternal
or just wrap it
mlvljr
@mlvljr - indeed, particularly if it's sealed.
Jeff Sternal
btw, updated the startpost
Seshiro
@Seshiro - I've updated my answer to explain how you could use this technique to solve your particular problem.
Jeff Sternal
+3  A: 

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