过时的属性导致财产被XmlSerialization被忽略

过时的属性导致财产被XmlSerialization被忽略

本文介绍了过时的属性导致财产被XmlSerialization被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重构某些对象序列化为XML,但需要保持向后兼容性的一些属性,我有一个旧的对象转换成新的,我和空过时的财产的方法。我想使用过时属性告诉其他开发人员不要使用此属性,但它造成的财产由的XmlSerializer

I'm refactoring some objects that are serialized to XML but need to keep a few properties for backwards compatibility, I've got a method that converts the old object into the new one for me and nulls the obsolete property. I want to use the Obsolete attribute to tell other developers not to use this property but it is causing the property to be ignored by the XmlSerializer.

类似code:

[Serializable]
public class MySerializableObject
{
    private MyObject _oldObject;
    private MyObject _anotherOldObject;

    private MyObject _newBetterObject;

    [Obsolete("Use new properties in NewBetterObject to prevent duplication")]
    public MyObject OldObject
    {
      get { return _oldObject; }
      set { _oldObject = value; }
    }

    [Obsolete("Use new properties in NewBetterObject to prevent duplication")]
    public MyObject AnotherOldObject
    {
      get { return _anotherOldObject; }
      set { _anotherOldObject = value; }
    }

    public MyObject NewBetterObject
    {
      get { return _anotherOldObject; }
      set { _anotherOldObject = value; }
    }
}

这是一个解决办法的任何想法?我最好的解决方法是在XML注释写过时...

Any ideas on a workaround? My best solution is to write obsolete in the XML comments...

更新:我使用.NET 2.0

推荐答案

您可以尝试以下解决方法:

You may try the following workaround:

添加一个名为方法

ShouldSerializeOldObject ()
{
   return true;
}

ShouldSerializeAnotherOldObject ()
{
   return true
}

这可能会覆盖过时的属性。

this may override the obsolete Attribute

这篇关于过时的属性导致财产被XmlSerialization被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:30