Ignore binary serialization on a property

Viewed 39136

I have a regular C# POCO. At the class level, I am decorating the object with [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?

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

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

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

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

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

'Or'

you can implement ISerializable and not serialize those field.

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


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

Cheat and use a method

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

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