Overwriting class constructor from outisde

So the question is simple for aks: How can I rewrite the class constructor from the outside. The problem itself is that I have a class that has already been compiled and it already has some constructors, but these coder idiots have removed the constructor, so now I can not execute XML (de) Serialize it ...

So what they did is:
They have changed Vector2 (); Vector2 (x, y); in Vector2 (x = 0, y = 0);

But my problem is that the Serializer is not smart enough to realize that it can still create a class, and changing the whole code will be a pain in * * *

+4
source share
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); 
+9
source

If for some reason the class has been marked as partial, you can add it with your own partial class declaration:

 public partial class CompiledClass { public CompiledClass() { } } 
+3
source

All Articles