serialize event handler

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

Hi,
I have a class to serialize. This class has come properties like this one

private myObject _listOf;
public myObject listOf
{
get {
if(_listOf==null)
{
_listOf=myClass.getMyObjectList();
}
return _listOf;
}
}

When I serialize this class and _listOf==null it calls the function
myClass.getMyObjectList().
But I want that when serialize it has to return the current value of the
property _listOf, if null too, without calls the funtion.
It's possible know when a class is "unser serialization"? and change the
code in something like that?
....
get {
if(this.underSerialization)return _listOf;
if(_listOf==null)
{
_listOf=myClass.getMyObjectList();
}
return _listOf;
}
....

Thank you.
Regards
 
If it is serializing properties, I'm guessing this is Xml
serialization, yes?

In which case, you can write a ShouldSerialize method, and return
"false" if it doesn't have a value (so shouldn't be serialized), and
"true" if it does (and should be serialized). Not sure how this
relates to event-handlers, though...

http://msdn.microsoft.com/en-us/library/53b8022e.aspx

Marc
 
I forgot to add; it needs to be public to work, but you can make it
invisible easily enough (below). Also note that the property will be
invoked during deserialization if the element is present, which might
be an issue; if so, you might have to drop down to IXmlSerializable...
anyway, a ShouldSerialize example...

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.ComponentModel;

class Program
{
static void Main()
{
XmlSerializer xser = new XmlSerializer(typeof(Foo));
Foo foo = new Foo { SomeProp = "abc" };

xser.Serialize(Console.Out, foo);

var obj = foo.Bars; // hit the property

xser.Serialize(Console.Out, foo);
}
}
[Serializable]
public class Bar
{
public string Name { get; set; }
}
[Serializable]
public class Foo
{
public string SomeProp { get; set; }
private List<Bar> bars;
public List<Bar> Bars
{
get
{
if (bars == null) bars = new List<Bar>();
return bars;
}
}
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public bool ShouldSerializeBars() { return bars != null; }
}
 
It works for the serialization but the problem remains bacause of the
deserialization that calls the get method of my property, and I don't know
why.
I supposed it calls only the set one. I'm looking for documentation about
that.

Thank you very mutch anyway.
 
With lists, it assumes that the class will populate the list;
essentially, it will use:

IList list = yourObj.SomeProp;
foreach(item in the xml) {
list.Add(item);
}

If you need something more complex, you'll have to use
IXmlSerializable and do it manually. I'm not aware of any special
deserialization markers (I will try to have a look later, though...)

Marc
 
I thought of a way to do it; use the Bars property (with the code to
load from a db or whatever) in your code, but use a separate Facade
for serialization; mark Bars as "ignore", and rename to facade to
"Bars" during serialization:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.ComponentModel;


class Program
{
static void Main()
{
XmlSerializer xser = new XmlSerializer(typeof(Foo));
Foo foo = new Foo { SomeProp = "abc" };


xser.Serialize(Console.Out, foo);


foo.Bars.Add(new Bar { Name = "Fred" });


xser.Serialize(Console.Out, foo);
}


}


[Serializable]
public class Bar
{
public string Name { get; set; }

}


[Serializable]
public class Foo
{
public string SomeProp { get; set; }
private List<Bar> bars;
[XmlIgnore]
public List<Bar> Bars
{
get
{
if (bars == null)
{
// todo: init list
bars = new List<Bar>();
Console.WriteLine("Bars: TODO: init list");
}
return bars;
}
}
[XmlElement("Bars")]
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public List<Bar> BarsFacade
{
get
{
if (bars == null)
{ // init as empty
bars = new List<Bar>();
}
return bars;
}
}
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public bool ShouldSerializeBarsFacade() { return bars != null; }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top