Search code examples
c#datacontractserializerdatacontract

What happens to constructor when used with DataContracts?


I have an object that i've serialised but I want to run a shuffling routine on the constructor will that extra function be run when the object is deserialized or do datacontracts not run the the constructor?


Solution

  • DataContractSerializer emits and eats XML. The default constructor is always invoked when deserializing this XML back to an instance of a class. So if you deserialize back to the same class the constructor code will be run.

    But if you have exposed this class in a WCF webservice and generated a client proxy, then this client proxy will not have any of the custom code that you have written in the methods or constructors.