Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does WCF deserialization instantiate objects without calling a constructor?

There is some magic going on with WCF deserialization. How does it instantiate an instance of the data contract type without calling its constructor?

For example, consider this data contract:

[DataContract] public sealed class CreateMe {    [DataMember] private readonly string _name;    [DataMember] private readonly int _age;    private readonly bool _wasConstructorCalled;     public CreateMe()    {       _wasConstructorCalled = true;    }     // ... other members here } 

When obtaining an instance of this object via DataContractSerializer you will see that the field _wasConstructorCalled is false.

So, how does WCF do this? Is this a technique that others can use too, or is it hidden away from us?

like image 808
Drew Noakes Avatar asked Oct 07 '08 14:10

Drew Noakes


People also ask

Which namespace is used for serialization in WCF?

DataContractSerializer as the Default By default WCF uses the DataContractSerializer class to serialize data types.

What serializer does WCF use?

Windows Communication Foundation (WCF) uses the DataContractSerializer as its default serialization engine to convert data into XML and to convert XML back into data.


1 Answers

FormatterServices.GetUninitializedObject() will create an instance without calling a constructor. I found this class by using Reflector and digging through some of the core .Net serialization classes.

I tested it using the sample code below and it looks like it works great:

using System; using System.Reflection; using System.Runtime.Serialization;  namespace NoConstructorThingy {     class Program     {         static void Main()         {             // does not call ctor             var myClass = (MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass));              Console.WriteLine(myClass.One); // writes "0", constructor not called             Console.WriteLine(myClass.Two); // writes "0", field initializer not called         }     }      public class MyClass     {         public MyClass()         {             Console.WriteLine("MyClass ctor called.");             One = 1;         }          public int One { get; private set; }         public readonly int Two = 2;     } } 

http://d3j5vwomefv46c.cloudfront.net/photos/large/687556261.png

like image 118
Jason Jackson Avatar answered Oct 08 '22 01:10

Jason Jackson