1

I use ISerializationSurrogate. In object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) obj will be the empty new object.

the MSDN says:

The SetObjectData method is called during deserialization. With this method, you can take the empty Object obj that has already been created, and enter SerializationInfo info data into that object. Constructors are not invoked during deserialization of information and reconstruction of the object.

My question is: how will the object be created? Perhaps someone can point me to the Code at http://www.sourceof.net where this happens?

BennoDual
  • 5,865
  • 15
  • 67
  • 153

1 Answers1

2

The object is created by the FormatterServices.GetUninitializedObject method. The code where this is called from is here.

The implementation of GetUninitializedObject is deep in the CLR itself, you can see the sources here if you are really interested.

If you have the sources of the object to de/serialize it is better to implement ISerializable and use the special constructor because you can set the readonly fields from there. When using surrogates you can set the read-only fields only by reflection, which is not just slow but requires full trust permission.

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65