Maybe not straightforward , but: if you add a hidden Area property, and
tell it never to serialize, it should work.
Note I've used C# 3 auto-implemented properties for Width/Height -
just replace with standard properties if you are using C# 2; but don't
use public fields... pretty much "ever".
Marc
using System;
using System.Componen tModel;
using System.IO;
using System.Xml.Seri alization;
[Serializable]
public sealed class MyClass
{
public int Width { get; set; }
public int Height { get; set; }
[EditorBrowsable (EditorBrowsabl eState.Never), Browsable(false )]
public int Area
{
get { return Width * Height; }
set { Width = Height = (int)Math.Sqrt( value); }
}
[EditorBrowsable (EditorBrowsabl eState.Never), Browsable(false )]
public bool ShouldSerialize Area() { return false; }
public override string ToString()
{
return string.Format(" {0}x{1}", Width, Height);
}
}
static class Program
{
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(t ypeof(MyClass)) ;
MyClass item = new MyClass { Width = 3, Height = 5 };
StringWriter writer = new StringWriter();
xs.Serialize(wr iter, item);
string xml = writer.ToString ();
Console.WriteLi ne(xml);
using (StringReader sr = new StringReader(@" <MyClass><Area> 25</
Area></MyClass>"))
{
MyClass oldStyle = (MyClass)xs.Des erialize(sr);
Console.WriteLi ne(oldStyle);
}
using (StringReader sr = new StringReader(@" <MyClass><Width >7</
Width><Height>3 </Height></MyClass>"))
{
MyClass newStyle = (MyClass)xs.Des erialize(sr);
Console.WriteLi ne(newStyle);
}
}
}
tell it never to serialize, it should work.
Note I've used C# 3 auto-implemented properties for Width/Height -
just replace with standard properties if you are using C# 2; but don't
use public fields... pretty much "ever".
Marc
using System;
using System.Componen tModel;
using System.IO;
using System.Xml.Seri alization;
[Serializable]
public sealed class MyClass
{
public int Width { get; set; }
public int Height { get; set; }
[EditorBrowsable (EditorBrowsabl eState.Never), Browsable(false )]
public int Area
{
get { return Width * Height; }
set { Width = Height = (int)Math.Sqrt( value); }
}
[EditorBrowsable (EditorBrowsabl eState.Never), Browsable(false )]
public bool ShouldSerialize Area() { return false; }
public override string ToString()
{
return string.Format(" {0}x{1}", Width, Height);
}
}
static class Program
{
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(t ypeof(MyClass)) ;
MyClass item = new MyClass { Width = 3, Height = 5 };
StringWriter writer = new StringWriter();
xs.Serialize(wr iter, item);
string xml = writer.ToString ();
Console.WriteLi ne(xml);
using (StringReader sr = new StringReader(@" <MyClass><Area> 25</
Area></MyClass>"))
{
MyClass oldStyle = (MyClass)xs.Des erialize(sr);
Console.WriteLi ne(oldStyle);
}
using (StringReader sr = new StringReader(@" <MyClass><Width >7</
Width><Height>3 </Height></MyClass>"))
{
MyClass newStyle = (MyClass)xs.Des erialize(sr);
Console.WriteLi ne(newStyle);
}
}
}
Comment