Creating Objects Without Calling the Constructor
When you use the new operator to create an instance of a class, the class constructor is called before the reference to the newly created object is returned. This also applies to the reflection scenarios where you call Activator.CreateInstance, which finds the right constructor based on the number, order and type of the parameters provided.
This is the expected behavior in most cases as you don’t want to work with an object that might be in an invalid state. However, there are cases where you don’t need to run the constructor. A good example is when you are deserializing an object from a byte stream. During deserialization, you will be using the values embedded in the incoming byte stream to initialize the member variables so there is no point in initializing those members in the constructor as they will be overwritten.
The formatters that ship with the .NET Framework (such as BinaryFormatter and DataContractSerializer) follow this approach as you can expect. There is a utility class called FormatterServices, which provides some of the underlying services to the formatters, including but not limited to object creation.
namespace System.Runtime.Serialization
{
public sealed class FormatterServices
{
public static void CheckTypeSecurity(Type t,
TypeFilterLevel securityLevel);
public static object[] GetObjectData(object obj,
MemberInfo[] members);
public static object GetSafeUninitializedObject(Type type);
public static MemberInfo[] GetSerializableMembers(Type type);
public static MemberInfo[] GetSerializableMembers(Type type,
StreamingContext context);
public static ISerializationSurrogate
GetSurrogateForCyclicalReference(
ISerializationSurrogate innerSurrogate);
public static Type GetTypeFromAssembly(Assembly assem,
string name);
public static object GetUninitializedObject(Type type);
public static object PopulateObjectMembers(object obj,
MemberInfo[] members, object[] data);
}
}
When you call GetUninitializedObject or GetSafeUninitializedObject, an instance of the class is created and returned without calling the constructor of the class. But note that the member variables are set to null and zero. FormatterServices is a public class so you can use its published functionality inside your application but you need to be very careful when using Get(Safe)UninitializedObject. As explained in the MSDN documentation, when you use these methods you should immediately populate all fields to make sure the object is not left in an invalid state.
There are some other interesting operations in FormatterServices. For example, GetSerializableMembers returns a list of members that will be serialized by the classic formatters.
One final point on this topic. If you are deserializing an object from a byte stream and want to run a piece of code to initialize the object before it is used (for example to calculate some of the fields that were not included in the serialized format), you will need to use the serialization attributes. See my earlier post on this subject for more information.
Originally posted by Mehran Nikoo on 23 April 2010 here https://mnikoo.net/2010/04/23/creating-objects-without-calling-the-constructor/