Separate "this" and the object that's actually being serialized. Say
normally you have a class like this:
[XmlRoot("Class")]
public class C {
private int _att;
[XmlAttribute] public int Att { get { return _att; } set { _att =
value; } }
}
Then separate it into two classes:
public class C {
private C_Data _data;
public int Att { get { return _data.Att; } set { _data.Att = value;
} }
}
[XmlRoot("Class")]
public class C_Data {
[XmlAttribute] publi int Att;
}
The calling code still just sees class C all the same, but internally
you use C_Data for serialization/deserialization and data storage.
Unfortunately C_Data actually has to be public, so put it in a
different namespace so as not to confuse callers.
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 22 Jan 2007 19:40:45 -0800, "Hoss" <(E-Mail Removed)> wrote:
>Quick Remark.
>
>I have a class that looks like this
>
>[Serializable]
>[XmlRoot("Class")]
>public Class
>{
> [XmlAttribute]
> public int Attribu
> {
> get{};set{};
> }
> [XmlAttribute]
> public int Attribu
> {
> get{};set{};
> }
>}
>
>Set up perfectly for Xml Serialization / Deserialization. One of the
>great things about this is that if I, say, add a column to the
>database, all I need to do is create the new public property on my
>class. I do not have to say add a few lines to my SerializeToXml()
>method, or add code to my DeserializeFromXml() method, since the
>System.Xml.Serialization.XmlSerializer determines all that information
>dynamically using Reflection. Okay, great, we have realized the
>flexibility benefits from that.
>
>But how do I set up my Deserialization constructor to work with that?
>Consider:
>
> public Class(string id)
> {
> string xml = FetchXml() // Get Xml representing this object
>from somewhere. The ID is the key on the data.
> Class instance = XmlSerializer.Deserialize(xml);
> this.Attrib = instance.Attrib;
> this.Attrib = instance.Attrib;
> this.Attrib = instance.Attrib;
> ... ect.
> }
>
>You cant say
>this = XmlSerializer.Deserialize(xml);
>
>So - is there any way to Deserialize from xml and populate all the
>properties in a CONSTRUCTOR without enumerating each property ?? I
>guess I could use a static factory instead of a constructor.
>
>Thoughts?
|