Conditionally serialize a member

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

I have a member of my class which I only want to serialize in a flag is
true. So in a way it would be ideal if I could do something like this:

if (flag == true)
NonSerializableAttrbiute(mymember);

Is there a simple way to do this?

Thanks,
Joe
 
Joe,

No, without custom serialization (implementation of the ISerializable
interface, as well as the custom serialization constructor), you can't do
it.
 
Joe said:
I have a member of my class which I only want to serialize in a flag is
true. So in a way it would be ideal if I could do something like this:

if (flag == true)
NonSerializableAttrbiute(mymember);

Is there a simple way to do this?

How about defining the "ShouldSerializeXYZ" method? See the documentation
of PropertyDescriptor.ShouldSerializeValue
 
Joe said:
Ben,

Would you have an example of using this on a DataTable?

Sorry, no, I haven't done databases since ASP (not .NET). My current job is
hardware interfacing.
 
DataTable has custom serialization, so I don't think this would work.
You could give it a go (by adding a "bool ShouldSerializeWhatever()")
but I don't see how it makes sense when serializing a DataTable...
i.e. missing out one value in (what is essentially) a grid...

Are you trying to do this for all rows, or just specific rows? If all,
perhaps consider removing the column (perhaps in a clone of the
DataSet/DataTable).

Marc
 
Back
Top