I have a class, which can be serialized using binary formatter. I want to have a two strategies for serializing. The second strategy should be different from the main one by excluding some specific fields from being serialized. How can I achieve this?
You can use NonSerializedAttribute on that field which you don't want to Serialize.
Also look at this MSDN article on this.
Create two memento classes each with the information you want to serialize, and add code to read in/out the values from the main class to the memento classes.
link to Memento strategy in Wikipedia.
link to dotfactory article on the strategy with C# and VB.NET
Example
Crate a class with two dependent properties, "number" and "square". Setting each one completely defines the class. Create two seriazation classes to serialize the main class two different ways:
class Data // The main class to store data in
{
int x;
public Data() { this.x = 0; }
public int Number
{
get { return x; }
set { x = value; }
}
public int Square
{
get { return x * x; }
set { x = (int)Math.Sqrt(value); }
}
public void FromNumberStore(NumberMemento mem)
{
this.Number = mem.Number;
}
public void FromSqureStore(SquareMemento mem)
{
this.Square = mem.Square;
}
}
[Serializable]
class NumberMemento // memento #1
{
int x;
public NumberMemento() { x = 0; }
public NumberMemento(Data data)
{
this.x = data.Number;
}
public int Number
{
get { return x; }
set { x = value; }
}
}
[Serializable]
class SquareMemento // memento #2
{
int x2;
public SquareMemento() { x2 = 0; }
public SquareMemento(Data data)
{
this.x2 = data.Square;
}
public int Square
{
get { return x2; }
set { x2 = value; }
}
}
class Program // Sample code to check all around serialization.
{
static void Main(string[] args)
{
Data store = new Data();
store.Number = 9;
{
// Write and read based on number
NumberMemento mem1 = new NumberMemento(store);
BinaryFormatter bf1 = new BinaryFormatter();
FileStream fs = new FileStream("numstore.dat", FileMode.Create);
bf1.Serialize(fs, mem1);
fs.Close();
// clear data and deserialize
store.Number = 0;
fs = new FileStream("numstore.dat", FileMode.Open);
mem1 = bf1.Deserialize(fs) as NumberMemento;
fs.Close();
store.FromNumberStore(mem1);
// check store.Number == 9
}
{
// Write and read based on square
SquareMemento mem2 = new SquareMemento(store);
BinaryFormatter bf2 = new BinaryFormatter();
FileStream fs = new FileStream("sqrstore.dat", FileMode.Create);
bf2.Serialize(fs, mem2);
fs.Close();
// clear data and deserialize
store.Number = 0;
fs = new FileStream("sqrstore.dat", FileMode.Open);
mem2 = bf2.Deserialize(fs) as SquareMemento;
fs.Close();
store.FromSqureStore(mem2);
// check store.Number == 9
}
}
}
Related
I've seen ways to list all variables in a certain class, but is there a way to set their values?
I want a method I can call, and it can reset all variables' values to false / 0 , I know I can manually set those to false / 0, but any change to their values would mess up everything, and I'm just seeing if there's anything more dynamic / easy way that this could be done ?
Currently I'm doing the following:
// These are just some of the vars that are here.
Error = false;
double GUM = 0, MUM = 0;
decimal Mass = 0, GAcc = 0, Fg = 0, FµsRes = 0, FµkRes = 0, Fµs = FµsNS.Value, Fµk = FµkNS.Value;
As others have mentioned, you could use Reflection to set all the class fields to their default values:
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var myClass = new MyClass();
myClass.Error = true;
myClass.GUM = 1;
myClass.MUM = 2;
myClass.Mass = 3.5m;
myClass.GAcc = 4.5m;
myClass.Fg = 5.5m;
ResetFields(myClass);
// All fields are reseted
}
public static void ResetFields(object source)
{
foreach (var fieldInfo in source.GetType().GetFields() )
{
fieldInfo.SetValue(source, GetDefault(fieldInfo.FieldType) );
}
}
public static object GetDefault(Type type)
{
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}
}
public class MyClass
{
public bool Error = false;
public double GUM = 0, MUM = 0;
public decimal Mass = 0, GAcc = 0, Fg = 0;
//etc etc
}
}
A better approach however, is to create a class to only hold the values and re-create it when needed:
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var myClass = new MyClass();
myClass.Variables = new MyVariables();
myClass.Variables.Error = true;
myClass.Variables.GUM = 1;
myClass.Variables.MUM = 2;
myClass.Variables.Mass = 3.5m;
myClass.Variables.GAcc = 4.5m;
myClass.Variables.Fg = 5.5m;
myClass.Variables = new MyVariables();
// All fields are reseted
}
}
public class MyVariables
{
public bool Error = false;
public double GUM = 0, MUM = 0;
public decimal Mass = 0, GAcc = 0, Fg = 0;
}
public class MyClass
{
public MyVariables Variables;
// etc etc
public int Xyz
{
get { return Variables.GUM + Variables.MUM; } // Use calculated properties
}
}
}
The GetDefault method is shown in this answer.
So I want to create constructor for my class EmployeeNodeClass that takes In EmployeeNodeClass object and copies it using a deepclone funtion:
public static T DeepClone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
to the new object.
At first i thought that it is as simple as
public EmployeeNodeClass(EmployeeNodeClass EMPND)
{
this = DeepClone(EMPND);
}
but then I got the error that this is readonly.
so how can I do it?
It can be done using NewtonSoft JsonConvert.PopulateObject:
Example:
class Entity
{
public List<int> List { get; set; }
public Entity(List<int> list)
{
List = list;
}
public Entity(Entity original)
{
string originalJson = JsonConvert.SerializeObject(original);
JsonConvert.PopulateObject(originalJson, this);
}
}
And using it:
static void Main(string[] args)
{
var entity1 = new Entity(new List<int> { 1, 2, 3 });
var entity2 = new Entity(entity1);
entity1.List[0] = 5;
Console.WriteLine(entity2.List[0]);
}
Note: since this uses NewtonSoft Json, it will only clone public, writable properties. So internal state in private fields (that are not associated with such properties) will be lost.
This question already has answers here:
Using XmlSerializer to serialize derived classes
(3 answers)
Closed 4 years ago.
I tried following the Microsoft website documentation for addressing the above question. However, I'm not able to find the answer. So I tried writing the code, but my object is not getting serialized when I added more derived classes.
Here goes the code:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
public class Orchestra
{
// public Instrument[] Instruments;
public List<Instrument> Instruments;
public int i;
public float f;
public string s1;
public string s2;
public B bc;
}
public class B
{
public double dd;
}
public class Instrument
{
public string Name;
}
public class Brass : Instrument
{
public bool IsValved;
}
public class Percussion : Instrument
{
public string name;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("Override.xml");
test.DeserializeObject("Override.xml");
}
public void SerializeObject(string filename)
{
/* Each overridden field, property, or type requires
an XmlAttributes object. */
XmlAttributes attrs = new XmlAttributes();
/* Create an XmlElementAttribute to override the
field that returns Instrument objects. The overridden field
returns Brass objects instead. */
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
attrs.XmlElements.Add(attr);
// attrs.XmlArrayItems.Add(attr);
attr.ElementName = "Percussion";
attr.Type = typeof(Percussion);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
// Create the XmlAttributeOverrides object.
XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();
/* Add the type of the class that contains the overridden
member and the XmlAttributes to override it with to the
XmlAttributeOverrides object. */
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestra), attrOverrides);
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create the object that will be serialized.
Orchestra band = new Orchestra();
// Create an object of the derived type.
//Brass i = new Brass();
//i.Name = "Trumpet";
//i.IsValved = true;
//Instrument[] myInstruments = { i };
//band.Instruments = myInstruments;
List<Instrument> myInstruments = new List<Instrument>();
myInstruments.Add(new Brass() { Name = "Trumpet", IsValved = true });
myInstruments.Add(new Percussion() { Name = "Percussion", name = "Mridangam" });
band.Instruments = myInstruments;
band.i = 128;
band.f = 5.678f;
band.s1 = "Hi!";
band.s2 = "GOOD!!!";
B b = new B();
b.dd = 2.35674848;
band.bc = b;
// Serialize the object.
s.Serialize(writer, band);
writer.Close();
}
public void DeserializeObject(string filename)
{
XmlAttributeOverrides attrOverrides =
new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();
// Create an XmlElementAttribute to override the Instrument.
XmlElementAttribute attr = new XmlElementAttribute();
attr.ElementName = "Brass";
attr.Type = typeof(Brass);
// Add the element to the collection of elements.
attrs.XmlElements.Add(attr);
attrOverrides.Add(typeof(Orchestra), "Instruments", attrs);
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestra), attrOverrides);
FileStream fs = new FileStream(filename, FileMode.Open);
Orchestra band = (Orchestra)s.Deserialize(fs);
Console.WriteLine(band.i);
Console.WriteLine(band.f);
Console.WriteLine(band.s1);
Console.WriteLine(band.s2);
Console.WriteLine(band.bc.dd);
Console.WriteLine("Brass:");
/* The difference between deserializing the overridden
XML document and serializing it is this: To read the derived
object values, you must declare an object of the derived type
(Brass), and cast the Instrument instance to it. */
//Brass b;
//Percussion p;
Brass b;
// Percussion p;
//b = (Brass)i;
// int ii = 0;
foreach (Instrument i in band.Instruments)
//foreach(Instrument i in band.List<Instrument>)
{
// int i = 0;
// ii++;
// if (ii == 1)
// {
b = (Brass)i;
Console.WriteLine(
b.Name + "\n" +
b.IsValved);
// }
/*if (ii == 2)
{
p = (Percussion)i;
Console.WriteLine(
p.Name + "\n" +
p.name);
}*/
}
}
}
I even tried using XmlArrayItem. Could anyone guide how to go about this?
You have to add a new class Instruments to your code. See code below :
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Xml;
public class Orchestras
{
public List<Orchestra> orchestras = new List<Orchestra>();
}
public class Orchestra
{
[XmlElement]
public List<Instrument> Instruments { get; set; }
public int i;
public float f;
public string s1;
public string s2;
public B bc;
}
public class B
{
public double dd;
}
[XmlInclude(typeof(Brass))]
[XmlInclude(typeof(Percussion))]
public class Instrument
{
public string Name;
}
public class Brass : Instrument
{
public bool IsValved;
}
public class Percussion : Instrument
{
public string name;
}
public class Run
{
const string FILENAME = #"c:\temp\test.xml";
public static void Main()
{
Run test = new Run();
test.SerializeObject(FILENAME);
test.DeserializeObject(FILENAME);
}
public void SerializeObject(string filename)
{
Orchestras orchastras = new Orchestras();
Orchestra orchestra1 = new Orchestra();
orchastras.orchestras.Add(orchestra1);
List<Instrument> instruments = new List<Instrument>() {
new Instrument() { Name = "Brass"},
new Instrument() { Name = "Percussion"}
};
orchestra1.Instruments = instruments;
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s = new XmlSerializer(typeof(Orchestras));
// Writing the file requires a TextWriter.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(filename,settings);
// Create the object that will be serialized.
Orchestra band = new Orchestra();
orchastras.orchestras.Add(band);
// Create an object of the derived type.
//Brass i = new Brass();
//i.Name = "Trumpet";
//i.IsValved = true;
//Instrument[] myInstruments = { i };
//band.Instruments = myInstruments;
List<Instrument> myInstruments = new List<Instrument>();
myInstruments.Add(new Brass() { Name = "Trumpet", IsValved = true });
myInstruments.Add(new Percussion() { Name = "Percussion", name = "Mridangam" });
band.Instruments = myInstruments;
band.i = 128;
band.f = 5.678f;
band.s1 = "Hi!";
band.s2 = "GOOD!!!";
B b = new B();
b.dd = 2.35674848;
band.bc = b;
// Serialize the object.
s.Serialize(writer, orchastras);
writer.Close();
}
public void DeserializeObject(string filename)
{
// Create the XmlSerializer using the XmlAttributeOverrides.
XmlSerializer s =
new XmlSerializer(typeof(Orchestras));
FileStream fs = new FileStream(filename, FileMode.Open);
Orchestras band = (Orchestras)s.Deserialize(fs);
Console.WriteLine(band.orchestras[1].i);
Console.WriteLine(band.orchestras[1].f);
Console.WriteLine(band.orchestras[1].s1);
Console.WriteLine(band.orchestras[1].s2);
Console.WriteLine(band.orchestras[1].bc.dd);
Console.WriteLine("Brass:");
/* The difference between deserializing the overridden
XML document and serializing it is this: To read the derived
object values, you must declare an object of the derived type
(Brass), and cast the Instrument instance to it. */
//Brass b;
//Percussion p;
Instrument b;
// Percussion p;
//b = (Brass)i;
// int ii = 0;
foreach (Instrument i in band.orchestras[1].Instruments)
//foreach(Instrument i in band.List<Instrument>)
{
// int i = 0;
// ii++;
// if (ii == 1)
// {
b = i;
Console.WriteLine(
b.Name + "\n");
// }
/*if (ii == 2)
{
p = (Percussion)i;
Console.WriteLine(
p.Name + "\n" +
p.name);
}*/
}
}
}
I am looking for a way to fast and simple implementation of this paradigm:
MyByteArray mb = new MyByteArray();
mb.Add<byte>(bytevalue);
mb.Add<float>(floatvalue);
mb.Add<string>(str);
mb.Add<MyClass>(object);
And then get byte[] from mb to send it as a byte packet via RPC call (to be decoded on the other side using the same technique).
I've found MemoryStream, but it looks like too overheaded for this simple operation.
Can you help me? Thank you.
What are you looking for is BinaryWritter. But it still needs a Stream to write on for pure logic reason. And the only Stream that fits in your need is MemoryStream.
Are you afraid of performance overhead ? You can create your MemoryStream from an existing byte array ;
byte [] buffer = new byte[1024];
using (var memoryStream = new MemoryStream(buffer))
{
using (var binaryWriter = new BinaryWriter(memoryStream))
{
binaryWriter.Write(1.2F); // float
binaryWriter.Write(1.9D); // double
binaryWriter.Write(1); // integer
binaryWriter.Write("str"); // string
}
}
// buffer is filled with your data now.
A tricky way to achieve this is to use a combination of builtin class in .net
class Program
{
static void Main(string[] args)
{
Program program = new Program();
var listBytes = new List<byte>();
listBytes.Add( program.CastToBytes("test"));
listBytes.Add(program.CastToBytes(5));
}
Note
for a custom object you have to define an implicit operator on how the properties or all the object should be converted
public byte[] CastToBytes<T>(T value)
{
//this will cover most of primitive types
if (typeof(T).IsValueType)
{
return BitConverter.GetBytes((dynamic)value);
}
if (typeof(T) == typeof(string))
{
return Encoding.UTF8.GetBytes((dynamic) value);
}
//for a custom object you have to define the rules
else
{
var formatter = new BinaryFormatter();
var memoryStream = new MemoryStream();
formatter.Serialize(memoryStream, value);
return memoryStream.GetBuffer();
}
}
}
This looks like the case for Protocol Buffers, you could look at at protobuf-net.
First, let's decorate the classes.
[ProtoContract]
class User
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(2)]
public string Name { get; set; }
}
[ProtoContract]
class Message
{
[ProtoMember(1)]
public byte Type { get; set; }
[ProtoMember(2)]
public float Value { get; set; }
[ProtoMember(3)]
public User Sender { get; set; }
}
Then we create our message.
var msg = new Message
{
Type = 1,
Value = 1.1f,
Sender = new User
{
Id = 8,
Name = "user"
}
};
And now, we can use ProtoBuf's serializer to do all our work.
// memory
using (var mem = new MemoryStream())
{
Serializer.Serialize<Message>(mem, msg);
var bytes = mem.GetBuffer();
}
// file
using (var file = File.Create("message.bin")) Serializer.Serialize<Message>(file, msg);
I have been using BinaryFormatter to serialise data to disk but it doesn't seem very scalable. I've created a 200Mb data file but am unable to read it back in (End of Stream encountered before parsing was completed). It tries for about 30 minutes to deserialise and then gives up. This is on a fairly decent quad-cpu box with 8Gb RAM.
I'm serialising a fairly large complicated structure.
htCacheItems is a Hashtable of CacheItems. Each CacheItem has several simple members (strings + ints etc) and also contains a Hashtable and a custom implementation of a linked list. The sub-hashtable points to CacheItemValue structures which is currently a simple DTO which contains a key and a value. The linked list items are also equally simple.
The data file that fails contains about 400,000 CacheItemValues.
Smaller datasets work well (though takes longer than i'd expect to deserialize and use a hell of a lot of memory).
public virtual bool Save(String sBinaryFile)
{
bool bSuccess = false;
FileStream fs = new FileStream(sBinaryFile, FileMode.Create);
try
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, htCacheItems);
bSuccess = true;
}
catch (Exception e)
{
bSuccess = false;
}
finally
{
fs.Close();
}
return bSuccess;
}
public virtual bool Load(String sBinaryFile)
{
bool bSuccess = false;
FileStream fs = null;
GZipStream gzfs = null;
try
{
fs = new FileStream(sBinaryFile, FileMode.OpenOrCreate);
if (sBinaryFile.EndsWith("gz"))
{
gzfs = new GZipStream(fs, CompressionMode.Decompress);
}
//add the event handler
ResolveEventHandler resolveEventHandler = new ResolveEventHandler(AssemblyResolveEventHandler);
AppDomain.CurrentDomain.AssemblyResolve += resolveEventHandler;
BinaryFormatter formatter = new BinaryFormatter();
htCacheItems = (Hashtable)formatter.Deserialize(gzfs != null ? (Stream)gzfs : (Stream)fs);
//remove the event handler
AppDomain.CurrentDomain.AssemblyResolve -= resolveEventHandler;
bSuccess = true;
}
catch (Exception e)
{
Logger.Write(new ExceptionLogEntry("Failed to populate cache from file " + sBinaryFile + ". Message is " + e.Message));
bSuccess = false;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (gzfs != null)
{
gzfs.Close();
}
}
return bSuccess;
}
The resolveEventHandler is just a work around because i'm serialising the data in one application and loading it in another (http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/e5f0c371-b900-41d8-9a5b-1052739f2521)
The question is, how can I improve this? Is data serialisation always going to be inefficient, am i better off writing my own routines?
I would personally try to avoid the need for the assembly-resolve; that has a certain smell about it. If you must use BinaryFormatter, then I'd simply put the DTOs into a separate library (dll) that can be used in both applications.
If you don't want to share the dll, then IMO you shouldn't be using BinaryFormatter - you should be using a contract-based serializer, such as XmlSerializer or DataContractSerializer, or one of the "protocol buffers" implementations (and to repeat Jon's disclaimer: I wrote one of the others).
200MB does seem pretty big, but I wouldn't have expected it to fail. One possible cause here is the object tracking it does for the references; but even then, this surprises me.
I'd love to see a simplified object model to see if it is a "fit" for any of the above.
Here's an example that attempts to mirror your setup from the description using protobuf-net. Oddly enough there seems to be a glitch working with the linked-list, which I'll investigate; but the rest seems to work:
using System;
using System.Collections.Generic;
using System.IO;
using ProtoBuf;
[ProtoContract]
class CacheItem
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(2)]
public int AnotherNumber { get; set; }
private readonly Dictionary<string, CacheItemValue> data
= new Dictionary<string,CacheItemValue>();
[ProtoMember(3)]
public Dictionary<string, CacheItemValue> Data { get { return data; } }
//[ProtoMember(4)] // commented out while I investigate...
public ListNode Nodes { get; set; }
}
[ProtoContract]
class ListNode // I'd probably expose this as a simple list, though
{
[ProtoMember(1)]
public double Head { get; set; }
[ProtoMember(2)]
public ListNode Tail { get; set; }
}
[ProtoContract]
class CacheItemValue
{
[ProtoMember(1)]
public string Key { get; set; }
[ProtoMember(2)]
public float Value { get; set; }
}
static class Program
{
static void Main()
{
// invent 400k CacheItemValue records
Dictionary<string, CacheItem> htCacheItems = new Dictionary<string, CacheItem>();
Random rand = new Random(123456);
for (int i = 0; i < 400; i++)
{
string key;
CacheItem ci = new CacheItem {
Id = rand.Next(10000),
AnotherNumber = rand.Next(10000)
};
while (htCacheItems.ContainsKey(key = rand.NextString())) {}
htCacheItems.Add(key, ci);
for (int j = 0; j < 1000; j++)
{
while (ci.Data.ContainsKey(key = rand.NextString())) { }
ci.Data.Add(key,
new CacheItemValue {
Key = key,
Value = (float)rand.NextDouble()
});
int tail = rand.Next(1, 50);
ListNode node = null;
while (tail-- > 0)
{
node = new ListNode
{
Tail = node,
Head = rand.NextDouble()
};
}
ci.Nodes = node;
}
}
Console.WriteLine(GetChecksum(htCacheItems));
using (Stream outfile = File.Create("raw.bin"))
{
Serializer.Serialize(outfile, htCacheItems);
}
htCacheItems = null;
using (Stream inFile = File.OpenRead("raw.bin"))
{
htCacheItems = Serializer.Deserialize<Dictionary<string, CacheItem>>(inFile);
}
Console.WriteLine(GetChecksum(htCacheItems));
}
static int GetChecksum(Dictionary<string, CacheItem> data)
{
int chk = data.Count;
foreach (var item in data)
{
chk += item.Key.GetHashCode()
+ item.Value.AnotherNumber + item.Value.Id;
foreach (var subItem in item.Value.Data.Values)
{
chk += subItem.Key.GetHashCode()
+ subItem.Value.GetHashCode();
}
}
return chk;
}
static string NextString(this Random random)
{
const string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789 ";
int len = random.Next(4, 10);
char[] buffer = new char[len];
for (int i = 0; i < len; i++)
{
buffer[i] = alphabet[random.Next(0, alphabet.Length)];
}
return new string(buffer);
}
}
Serialization is tricky, particularly when you want to have some degree of flexibility when it comes to versioning.
Usually there's a trade-off between portability and flexibility of what you can serialize. For example, you might want to use Protocol Buffers (disclaimer: I wrote one of the C# ports) as a pretty efficient solution with good portability and versioning - but then you'll need to translate whatever your natural data structure is into something supported by Protocol Buffers.
Having said that, I'm surprised that binary serialization is failing here - at least in that particular way. Can you get it to fail with a large file with a very, very simple piece of serialization code? (No resolution handlers, no compression etc.)
Something that could help is cascade serializing.
You call mainHashtable.serialize(), which return a XML string for example. This method call everyItemInYourHashtable.serialize(), and so on.
You do the same with a static method in every class, called 'unserialize(String xml)', which unserialize your objetcs and return an object, or a list of objects.
You get the point ?
Of course, you need to implement this method in every of your class you want to be serializable.
Take a look at ISerializable interface, which represent exaclty what I'm describing. IMO, this interface looks too "Microsoft" (no use of DOM, etc), so i created mine, but principle is the same : cascade.