简体   繁体   English

为什么在WCF反序列化器初始化对象时不调用我的抽象基类的构造函数?

[英]Why is my abstract base class's constructor not called when an object is initialized by the WCF deserializer?

Question in title... In short - I have a WCF service exposing operations that return entity classes. 标题中的问题...简而言之 - 我有一个WCF服务公开返回实体类的操作。 The client-side classes inherit from an abstract base class instead of the default System.Object. 客户端类继承自抽象基类而不是默认的System.Object。 The abstract base class has a default constructor defined. 抽象基类定义了默认构造函数。 When calling one of the service methods I would expect that constructor to get called when the datacontract serializer materialize the returned objects. 当调用其中一个服务方法时,我希望在datacontract序列化器实现返回的对象时调用构造函数。 However, the constructor is not called. 但是,不会调用构造函数。 If on the other hand I create an instance of the entity class myself then the abstract class constructor is called. 另一方面,如果我自己创建实体类的实例,则调用抽象类构造函数。

Why, oh why, and is there a workaround? 为什么,为什么,是否有解决方法? Or did I miss something - is there another constructor signature that is called by the datacontract serializer when materializing objects? 或者我错过了什么 - 是否有另一个构造函数签名由datacontract序列化程序在实现对象时调用? If not, how can the datacontract serializer materialize objects without calling the constructors the same way that a "new SomeClass()" call would do? 如果没有,datacontract序列化程序如何在不调用构造函数的情况下实现对象,就像“new SomeClass()”调用一样? Or did I drink too much coffee today (only had 2 or 3 cups so far)? 或者我今天喝了太多咖啡(到目前为止只有2或3杯)?

WCF (and DataContractSerializer in particular) doesn't use constructors. WCF(特别是DataContractSerializer )不使用构造函数。 No, really (it uses FormatterServices.GetUninitializedObject to create raw objects). 不,真的(它使用FormatterServices.GetUninitializedObject来创建原始对象)。

It is expected that all data will be initialized either by the serializer, or for non-serialized fields - by serialization callbacks that you add (for example, via [OnDeserialized] ). 预计所有数据都将由序列化程序或非序列化字段初始化 - 通过您添加的序列化回调(例如,通过[OnDeserialized] )。

I fully understand the reasons, however I do not understand why they do not support serialization callbacks in Silverlight. 我完全理解原因,但是我不明白他们为什么不支持Silverlight中的序列化回调。 It seems to me, that in a WCF - Silverlight communication I cannot initialize my data contract without hacking myself. 在我看来,在WCF - Silverlight通信中,我无法在不自我攻击的情况下初始化我的数据合同。 So, if I had a private member in my base class for internal use (eg. undo-redo behavior), cannot use the default constructor: 所以,如果我的基类中有一个私有成员供内部使用(例如undo-redo行为),则不能使用默认构造函数:

Stack<PropertyChange> UndoStack = new Stack<PropertyChange>();

This simply does not work. 这根本行不通。 To make it work I should write something like this: 为了使它工作,我应该写这样的东西:

Stack<PropertyChange> _UndoStack;
Stack<PropertyChange> UndoStack
{
     get
     {
           return _UndoStack == null ? (_UndoStack = new Stack<PropertyChange>()) : _UndoStack;
     }
}

It seems a workaround to me. 这对我来说似乎是一种解决方法。 Anyone has better ideas? 谁有更好的想法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM