C# 忽略属性上的二进制序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2335113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:32:14  来源:igfitidea点击:

Ignore binary serialization on a property

c#serialization

提问by Jessy Houle

I have a regular C# POCO. At the class level, I am decorating the object with [Serializable()].

我有一个普通的 C# POCO。在类级别,我用[Serializable()].

That said, I am using the Linq Sum()on one of the properties and I am receiving an error upon serialization. If possible, I would like to just simply ignore this property. However, the [XmlIgnore()]is only for Xml Serialization, not Binary. Any ideas or thoughts?

也就是说,我Sum()在其中一个属性上使用 Linq ,并且在序列化时收到错误。如果可能,我只想简单地忽略此属性。但是,[XmlIgnore()]仅适用于 Xml 序列化,而不适用于二进制。有什么想法或想法吗?

The code is something like this, where I would like to ignore ValueTotal:

代码是这样的,我想忽略ValueTotal

[Serializable()]
public class Foo
{
  public IList<Number> Nums { get; set; }

  public long ValueTotal
  {
    get { return Nums.Sum(x => x.value); }
  }
}

采纳答案by Scoregraphic

Cheat and use a method

作弊和使用方法

[Serializable()]
public class Foo
{
  public IList<Number> Nums { get; set; }

  public long GetValueTotal()
  {
    return Nums.Sum(x => x.value);
  }
}

回答by Henrik


    [NonSerialized]
    private IList<Number> nums;
    public IList<Number> Nums { get {return nums;} set { nums = value; }  } 

回答by ata

ValueTotalis already ignored. Only data is serialized, not methods. Properties are methods actually.

ValueTotal已经被忽略了。只有数据被序列化,而不是方法。属性实际上是方法。

If you wish to ignore fields and not serialize them mark them as [NonSerialized].

如果您希望忽略字段而不将它们序列化,请将它们标记为[NonSerialized].

'Or'

'或者'

you can implement ISerializableand not serialize those field.

您可以实现ISerializable而不是序列化这些字段。

Here is some sample code on how can implement ISerializableand serialize data: http://www.c-sharpcorner.com/UploadFile/yougerthen/102162008172741PM/1.aspx

下面是一些关于如何实现ISerializable和序列化数据的示例代码:http: //www.c-sharpcorner.com/UploadFile/yougerthen/102162008172741PM/1.aspx

回答by Mark Cidade

Implement the ISerializableinterface and then use [XmlIgnore]for XML serialzation in the GetObjectData()method but then output as binary. It's actually simpler than how I just described it.

实现ISerializable接口,然后[XmlIgnore]GetObjectData()方法中用于 XML 序列化,然后输出为二进制。它实际上比我刚刚描述的更简单。

For ideas, see http://www.c-sharpcorner.com/UploadFile/yougerthen/102162008172741PM/1.aspx

有关想法,请参阅http://www.c-sharpcorner.com/UploadFile/yougerthen/102162008172741PM/1.aspx

回答by Bostwick

There is another way that is not listed here that has some benefits(the code below was made to support both binary serialization and xml)(for your example you would need a custom class to serialize your interfaces):

这里没有列出的另一种方法有一些好处(下面的代码支持二进制序列化和 xml)(对于您的示例,您需要一个自定义类来序列化您的接口):

    [OnSerializing]
    private void OnSerializing(StreamingContext context)
    {
        xmlShape4Port = new xmlStreamShape(shape4Port);
        shape4Port = null;
    }
    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
        if (xmlShape4Port != null)
        {
            shape4Port = xmlShape4Port.getShapeFromSaved();
            xmlShape4Port = null;
        }
    }

    [XmlIgnore()]
    public virtual StreamShape shape4Port {get;set;}

    [XmlElement("shape4Port")]
    public xmlStreamShape xmlShape4Port
    {
        get
        {
            if (shape4Port == null)
                return null;
            else
            {
                return new xmlStreamShape(shape4Port);
            }
        }
        set
        {
            shape4Port = value.getShapeFromSaved();
        }
    }