views:

507

answers:

4

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); }
  }
}

Thank you in advanced for any help.

-Jessy Houle

+2  A: 

Cheat and use a method

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

  public long GetValueTotal()
  {
    return Nums.Sum(x => x.value);
  }
}
Scoregraphic
+1 while I sometimes struggle with Properties being equal to state, and state is sometimes calculated, I'm starting to agree more that if you are doing calculations, then you should use a method as it shows the intent that this value is the result of a calculation. The truth is, why would you serialize a value that is calculated, shouldn't you serialize the data that you want, and then just redo the calculation on the other side?
NerdFury
I agree NerdFury, this does communicate my intentions better. Thank you. -Jessy Houle
Jessy Houle
How does this solve the problem "receiving an error upon serialization"?I assumed Number was not serializable an thus serializing the backing field of the Nums property would fail.
Henrik
IMO this is just plain wrong. `BinaryFormatter` doesn't look at methods or properties - it only looks at fields. Changing a property to a field should not *in any way* affect how `BinaryFormatter` works here.
Marc Gravell
Maybe it was because of the fact that he was using auto properties.
cornerback84
@NerdFury Sometimes you want some kind of calculated data available for display in data binding. In this case, a method is no good.
Benny Jobigan
@Benny Jobigan: I agree, but should a class which displays values really need to be serialized? I think the data (model) should be serialized and not its presentation
Scoregraphic
@Scoregraphic: eh, maybe, but it's a pain writing viewmodel wrapper classes for every data object I might have. Sometimes it's easier just to bind directly to the object.
Benny Jobigan
+2  A: 

    [NonSerialized]
    private IList<Number> nums;
    public IList<Number> Nums { get {return nums;} set { nums = value; }  } 
Henrik
Read again. He likes to ignore the ValueTotal-Property, not the list
Scoregraphic
No need to ignore ValueTotal property. Binary serialization serializes fields, not properties
Henrik
+4  A: 

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

cornerback84
A: 

Implement the ISerializable interface 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.

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

Mark Cidade
actually he don't need to implement the interface. He wanted to ignore ValueTotal property.
cornerback84