Search code examples
.netserializationinitializationobject-construction

How do you construct an object without initializing it? (.net)


When you new an object in C# a few things must happen:

  1. memory for the object is created, and whatever other book-keeping CLR whats to do
  2. fields are initialized to default values
  3. the constructor is invoked

Serialization frameworks seem to have some magical way to do 1 without doing 2 and 3. Or maybe it's not so magical after all. How would you do the same (skip 2 and 3) if you are writing your own deserialization code?


Solution

  • Binary formatter uses methods of FormatterServices, like GetUninitializedObject.