WCF反序列化如何在不调用构造函数的情况下实例化对象?

Dre*_*kes 78 .net c# reflection wcf serialization

WCF反序列化有一些神奇之处.如何在不调用其构造函数的情况下实例化数据协定类型的实例?

例如,考虑这个数据合同:

[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
}
Run Code Online (Sandbox Code Playgroud)

通过DataContractSerializer您获取此对象的实例时,您将看到该字段_wasConstructorCalledfalse.

那么,WCF如何做到这一点?这是其他人可以使用的技术,还是隐藏在我们之外?

Jas*_*son 100

FormatterServices.GetUninitializedObject()将在不调用构造函数的情况下创建实例.我通过使用Reflector并挖掘了一些核心的.Net序列化类来找到这个类.

我使用下面的示例代码对其进行了测试,看起来效果很好:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 很奇怪 - 我在linqpad中运行该代码,我得到:0 0作为输出.实际上,这对我来说很有意义,因为字段初始化器被内联到ctors AFAIK中 (6认同)
  • 好吧,我以前发错了答案(现已删除),所以我感到内疚.没有什么能比得上程序员的自负,让他做一些研究. (5认同)
  • 现在还有其他人想知道,FormatterServices.GetUninitializedObject是如何工作的呢?反射? (2认同)

小智 19

是的,FormatterServices.GetUninitializedObject()是神奇的源头.

如果要进行任何特殊初始化,请参阅此. http://blogs.msdn.com/drnick/archive/2007/11/19/serialization-and-types.aspx

  • 引用+1,`[OnDeserialized]`是我的解决方案! (3认同)