How does WCF deserialization instantiate objects without calling a constructor?
Asked Answered
K

2

80

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?

Komarek answered 7/10, 2008 at 14:15 Comment(0)
B
103

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

Boren answered 7/10, 2008 at 17:20 Comment(4)
Is anyone else now wondering, how does FormatterServices.GetUninitializedObject work, then? Reflection?Cecilia
If I recall its a call into native code. I couldn't follow this any farther down the rabbit hole with Reflector.Boren
Weird - I run that code in linqpad and I got: 0 0 as output. Actually that makes sense to me since field initializers are inlined into ctors AFAIKSubglacial
@Subglacial is correct. I've posted a screenshot with the code and result here. At first I thought it might be a difference in the .NET framework versions (since the answer is already 4 years old) but I checked for 2.0 and 4.0 and they both write 0 and 0 to the console. Jason Jackson, Could you update your post to reflect these findings?Callicrates
A
20

Yes, FormatterServices.GetUninitializedObject() is the source of the magic.

If you want to do any special initialization, see this. http://blogs.msdn.com/drnick/archive/2007/11/19/serialization-and-types.aspx

Appurtenant answered 2/4, 2010 at 17:41 Comment(1)
+1 for the reference, [OnDeserialized] was the solution for me!Ledge

© 2022 - 2024 — McMap. All rights reserved.