【问题标题】:How does WCF deserialization instantiate objects without calling a constructor?WCF反序列化如何在不调用构造函数的情况下实例化对象?
【发布时间】:2010-09-15 18:31:37
【问题描述】:

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
}

当通过DataContractSerializer获取该对象的实例时,您会看到_wasConstructorCalled字段为false

那么,WCF 是如何做到这一点的呢?这是其他人也可以使用的技术,还是对我们隐藏起来?

【问题讨论】:

    标签: c# .net wcf reflection serialization


    【解决方案1】:

    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;
        }
    }
    

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

    【讨论】:

    • 好吧,我之前发布了一个错误的答案(现已删除),所以我感到内疚。没有什么比伤害程序员的自尊心让他做一些研究更重要的了。
    • 现在还有人想知道 FormatterServices.GetUninitializedObject 是如何工作的吗?反射?
    • 如果我记得它是对本机代码的调用。我无法用 Reflector 在兔子洞里更进一步。
    • 奇怪 - 我在 linqpad 中运行该代码,我得到:0 0 作为输出。实际上这对我来说很有意义,因为字段初始值设定项被内联到 ctors AFAIK
    • @bushed 是正确的。我已经发布了带有代码和结果here 的屏幕截图。起初我认为 .NET 框架版本可能有所不同(因为答案已经有 4 年历史了),但我检查了 2.0 和 4.0,它们都将 0 和 0 写入控制台。 Jason Jackson,您能否更新您的帖子以反映这些发现?
    【解决方案2】:

    是的,FormatterServices.GetUninitializedObject() 是魔法的源泉。

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

    【讨论】:

    • +1 供参考,[OnDeserialized] 是我的解决方案!
    猜你喜欢
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2016-11-18
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多