DataContractSerializer doesn't call my constructor?

后端 未结 4 1464
感动是毒
感动是毒 2020-11-27 14:49

I just realized something crazy, which I assumed to be completely impossible : when deserializing an object, the DataContractSerializer doesn\'t call the constructor

4条回答
  •  無奈伤痛
    2020-11-27 15:31

    DataContractSerializer (like BinaryFormatter) doesn't use any constructor. It creates the object as empty memory.

    For example:

        Type type = typeof(Customer);
        object obj = System.Runtime.Serialization.
            FormatterServices.GetUninitializedObject(type);
    

    The assumption is that the deserialization process (or callbacks if necessary) will fully initialize it.

提交回复
热议问题