Saturday, August 13, 2005 6:38 PM
Olaf Conijn
Look dad, without calling any constructors!
While playing around some more with my ReflectionSerializationSurrogate I found that it did a good job on deserializing objects without default constructors. That’s a pretty cool but strange behavior, don’t you think?
After having another look in the documentation, I found the following method ‘FormatterServices. GetSafeUninitializedObject(Type)’. This method allows IFormatters to create an uninitialized Object before unserializing its contents.
Off course there aren’t a lot of scenarios this might be useful, in most scenarios you even would like to avoid calling such a method.
From the MSDN documentation:
“Because the new instance of the object is initialized to zero and no constructors are run, the object might not represent a state that is regarded as valid by that object. The current method should only be used for deserialization when the user intends to immediately populate all fields. It will not create an uninitialized string, since creating an empty instance of an immutable type serves no purpose.”
I found a useful scenario in dealing with circular references in my ServiceContainer, which uses constructor injection as its method to inject dependencies. I'll implement circular references on its second iteration 