Esempio n. 1
0
        public void WriteXml(XmlWriter writer)
        {
            // Go over all fields
            Type type = GetType();

            while (type != typeof(object))
            {
                FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType == typeof(EventHandler))
                    {
                        continue;
                    }

                    if (field.FieldType == typeof(TimeSpan))
                    {
                        TimeSpanSerializer serializer = new TimeSpanSerializer();
                        serializer.Serialize(writer, (TimeSpan)field.GetValue(this));
                    }
                    else
                    {
                        XmlSerializer serializer = new XmlSerializer(field.FieldType);
                        serializer.Serialize(writer, field.GetValue(this));
                    }
                }
                type = type.BaseType;
            }
        }
Esempio n. 2
0
        public void ReadXml(XmlReader reader)
        {
            // Move to the first value
            reader.Read();

            // Go over all fields
            Type type = GetType();

            while (type != typeof(object))
            {
                FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType == typeof(EventHandler))
                    {
                        continue;
                    }

                    if (field.FieldType == typeof(TimeSpan))
                    {
                        TimeSpanSerializer serializer = new TimeSpanSerializer();
                        field.SetValue(this, serializer.Deserialize(reader));
                    }
                    else
                    {
                        XmlSerializer serializer = new XmlSerializer(field.FieldType);
                        field.SetValue(this, serializer.Deserialize(reader));
                    }
                }

                type = type.BaseType;
            }
            // Move to the next node
            reader.Read();
        }