防止属性的序列化

时间:2017-04-10 17:17:02

标签: c# vb.net serialization

这比在SO上发布的其他类似问题中讨论的直截了当的情景要复杂一些。

我有这样的类层次结构:

  • DrawingObjectabstract类,定义abstract属性SizeLocation
    • Label(继承DrawingObject。提供SizeLocation的具体实现。两个属性都应序列化)
    • Line(继承DrawingObjectSizeLocation属性应在序列化/反序列化中忽略)

我使用DataContractSerializer序列化我的DrawingObject,这会带来以下问题:

  • 如果我没有使用DataContract标记任何类,则IgnoreDataMember无效,并Location / Size属性序列化LabelLine。我不想要的东西。
  • 如果我对我的类应用DataContract,则会生成一个运行时异常,告知DrawingObject无法用DataContract标记,因为它的基类ObservableObject(是的, MVVM Light)未标记DataContract属性。

如何防止在一个派生类中对这些属性进行序列化而不在另一个派生类中进行序列化?

修改

我挖的越多,它就越奇怪。看起来.NET Framework 3.5略微更改了规则,[DataContract][DataMember]属性为no longer required以使DataContractSerializer工作。如果省略这些属性,DataContractSerializer将序列化类的所有公共读/写属性(类必须具有公共参数构造函数)。这对我的场景来说可能是个好消息,但在这方面看起来C#和VB.NET的行为有点不同:

C#

以下代码正确序列化:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;

namespace ConsoleApp1
{
  public abstract class DrawingObject
  {
    public abstract string Location { get; set; }
    public abstract string Size { get; set; }
  }

  public class Label : DrawingObject
  {
    public Label() { }

    private string _Loc;
    private string _Sz;

    public override string Location { get { return _Loc; } set { _Loc = value; } }
    public override string Size { get { return _Sz; } set { _Sz = value; } }
  }

  public class Line : DrawingObject
  {
    public Line() { }

    public override string Location { get { return "Line Location"; } set { Console.WriteLine("Line.Location.set"); } }
    public override string Size { get { return "Line Size"; } set { Console.WriteLine("Line.Size.set"); } }
  }

  class Program
  {
    static void Main(string[] args)
    {
      DrawingObject D1 = new Label() { Location="Label Loc", Size="Label Sz" } ;
      DrawingObject D2 = new Line();

      List<DrawingObject> DObjs = new List<DrawingObject>();
      DObjs.Add(D1);
      DObjs.Add(D2);

      DataContractSerializer S = new DataContractSerializer(typeof(List<DrawingObject>), new[] { typeof(Line), typeof(Label) }, 0x7FFF, false, true, null);

      var sb = new System.Text.StringBuilder();

      using (var writer = new StringWriter(sb))
      {
        using (var xmlWriter = System.Xml.XmlWriter.Create(writer, new System.Xml.XmlWriterSettings() { Indent = true, OmitXmlDeclaration = false }))
          S.WriteObject(xmlWriter, DObjs);

        Console.WriteLine(sb.ToString());

        Console.Read();
      }
    }
  }
}

VB.NET

此代码不会对任何内容进行序列化:

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Xml.Serialization

Public MustInherit Class DrawingObject
  Public MustOverride Property Location() As String
  Public MustOverride Property Size() As String
End Class

Public Class Label
  Inherits DrawingObject

  Public Sub New()
  End Sub

  Private _Loc As String
  Private _Sz As String

  Public Overrides Property Location() As String
    Get
      Return _Loc
    End Get
    Set
      _Loc = Value
    End Set
  End Property

  Public Overrides Property Size() As String
    Get
      Return _Sz
    End Get
    Set
      _Sz = Value
    End Set
  End Property
End Class

Public Class Line
  Inherits DrawingObject

  Public Sub New()   
  End Sub

  Public Overrides Property Location() As String
    Get
      Return "Line Location"
    End Get
    Set
      Console.WriteLine("Line.Location.set")
    End Set
  End Property

  Public Overrides Property Size() As String
    Get
      Return "Line Size"
    End Get
    Set
      Console.WriteLine("Line.Size.set")
    End Set
  End Property
End Class

Module Module1
  Sub Main()
    Dim D1 As DrawingObject = New Label() With {.Location = "Label Loc", .Size = "Label Sz"}
    Dim D2 As DrawingObject = New Line()

    Dim DObjs As New List(Of DrawingObject)
    DObjs.Add(D1)
    DObjs.Add(D2)

    Dim S As New DataContractSerializer(GetType(List(Of DrawingObject)), {GetType(Line), GetType(Label)}, &H7FFF, False, True, Nothing)

    Dim sb = New System.Text.StringBuilder()

    Using writer = New StringWriter(sb)
      Using xmlWriter = System.Xml.XmlWriter.Create(writer, New System.Xml.XmlWriterSettings() With {.Indent = True, .OmitXmlDeclaration = False})
        S.WriteObject(xmlWriter, DObjs)

        Console.WriteLine(sb.ToString())
        Console.Read()
      End Using
    End Using
  End Sub

End Module

我试图让它们在语法上等效,但DataContractSerializer表现不同。

编辑2

我测试了@ CodeCaster的建议,并将[IgnoreDataMember]应用于Location对象的Line属性。没有任何区别(Location属性仍为Line序列化。看起来DataContractSerializer在派生类中不尊重此属性。我还尝试直接序列化Line对象而不是父List。然后Location被写入输出。

不知道从哪里开始。

编辑3

经过一天的挖掘和尝试一切,上面的c#和VB.NET代码之间的差异最终证明是在刷新XML编写器时的一个问题。奇怪的是,C#代码并不要求我在序列化对象后调用Flush(),而VB.NET只在Flush()之后调用WriteObject()时产生输出。 / p>

我发现的另一件事是IgnoreDataMember对派生类中被覆盖的成员没有任何影响。您必须在基类中应用该属性才能使其工作,这在我的情况下当然是不可能的。我想我必须围绕这个问题发明一些黑客。

1 个答案:

答案 0 :(得分:0)

您必须使用“KnownType”才能使DataContractSerializer执行正确的序列化,因此您的base classe需要具有DataContract属性并通知子类类型,如示例

[DataContract]
[KnownType(typeof(Label))]
public abstract class DrawingObject
{
public abstract string Location { get; set; }
public abstract string Size { get; set; }
}