忽略要(XML)序列化的属性 [英] Ignore Property to be (XML) serialized

查看:334
本文介绍了忽略要(XML)序列化的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的课程有一些我不想序列化的属性。

My class has some properties that I don't want to be (XML) serialized.

示例:

public class MyClass
{
    private TimeSpan executionTime = TimeSpan.FromSeconds(0.0);

    public TimeSpan ExecutionTime
    {
        get {return executionTime;}
        set {executionTime = value;}
    }

    public double ExecutionTimeSeconds
    {
        get {return executionTime.TotalSeconds;}
        set {executionTime = TimeSpan.FromSeconds(value);}
    }
}

此外,这是一些无用的代码,您不希望这两种方法都被(反)序列化。当几个公共可读和可写属性相互影响时,总是会发生此问题。

Apart from that this is a bit of useless code, You don't want both methods to be (de)serialized. This problem occurs always when several public readable and writable properties affect each other.

如何防止这种情况发生?

How to prevent this?

推荐答案

二进制序列化和xml序列化之间有区别

There is a difference between binary serialization and xml serialization


  • 二进制序列化(反)对所有序列化(反序列化)公共和非公共)字段

  • 标准XML序列化(反序列化)公共可读和可写属性

二进制序列化

您的示例类不会在二进制序列化中带来问题。如果您的班级有两个相互关联的字段,那甚至不会导致问题:

Your example class wouldn't give a problem in binary serialization. It wouldn't even lead to problems if your class would have two fields that have a relation to each other:

class BinaraySerializationProblem
{
    private int x = 0;
    private int doubleX = 0;
    // etc. Methods that change x and doubleX
}

当您处理x和doubleX之间的关系时,二进制序列化将不是问题。

As long as you take care of the relation between x and doubleX, binary serialization wouldn't be a problem.

但是有时您不希望序列化某些项以提高效率原因:

However sometimes you don't want some items to be serialized for efficiency reasons:

[Serializable]
class MyImage
{
    private string imageFileName = null;
    private BitMap loadedImage = null;
    // etc. Methods to change imageFileName and load the image
}

进行序列化时,您可能不想保存位图,因为可以通过读取文件来重新创建位图。在这种情况下,您可以将属性[NonSerialized]添加到字段(不是属性!)

When serializing this, you probably don't want to save the bitmap, because it can be recreated by reading the file. In that case you'll add the attribute [NonSerialized] to the field (not the property!)

[Serializable]
class MyImage
{
    private string imageFileName = null;

    [NonSerialized]
    private BitMap loadedImage = null;
    // etc. Methods to change imageFileName and load the image
}

可以在名称空间系统中找到[Serializable]和[NonSerialized]

[Serializable] and [NonSerialized] can be found in namespace System

XML序列化

保存XML时,将对所有公共可读和可写属性的所有ToString()值进行序列化。

When saving XML, all ToString() values of all public readable and writable attributes are serialized.

因此,对于XML,ExecutionTime和ExecutionTimeSeconds的值都将为已保存。

So in case of XML both the values of ExecutionTime and ExecutionTimeSeconds will be saved.

如果您不希望序列化属性,请使用属性[XmlIgnore]。更改您的课程,如下所示:

If you don't want a property to be serialized use the attribute [XmlIgnore]. Change your class as follows:

[Serializable]
public class MyClass
{
    private TimeSpan executionTime = TimeSpan.FromSeconds(0.0);

    [System.Xml.Serialization.XmlIgnore] 
    public TimeSpan ExecutionTime
    {
        get {return executionTime;}
        set {executionTime = value;}
    }

    public double ExecutionTimeSeconds
    {
        get {return executionTime.TotalSeconds;}
        set {executionTime = TimeSpan.FromSeconds(value);}
    }
}

这会导致属性ExecutionTime的值不是

This has the effect that the value of property ExecutionTime is not serialized, only the value of ExecutionTimeSeconds is.

顺便说一句,我注意到TimeSpan类不是XML可序列化的,所以如果您需要对时间进行xml序列化,时间跨度,必须添加[XmlIgnore]属性并创建一个额外的属性,以将TimeSpan保存为可序列化的格式(秒/毫秒/小时/等)。

By the way, I've noticed that class TimeSpan is not XML-serializable, so if you need to xml serialize a time span, it is necessary to add [XmlIgnore] the property and create an extra property to save the TimeSpan in a serializable format (seconds / msec / hours / etc).

这篇关于忽略要(XML)序列化的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆