protected bool ShouldSerializeAsAttribute(PropertyInfo pi, object parent)
 {
     if (parent != null)
     {
         PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(parent)[pi.Name];
         if (propertyDescriptor != null)
         {
             SerializationVisibilityAttribute serializationVisibilityAttribute = (SerializationVisibilityAttribute)propertyDescriptor.Attributes[typeof(SerializationVisibilityAttribute)];
             if (serializationVisibilityAttribute != null)
             {
                 if (serializationVisibilityAttribute.Visibility == SerializationVisibility.Attribute)
                 {
                     return(true);
                 }
                 if (serializationVisibilityAttribute.Visibility == SerializationVisibility.Element)
                 {
                     return(false);
                 }
             }
         }
     }
     if (!pi.PropertyType.IsClass)
     {
         return(true);
     }
     if (pi.PropertyType == typeof(string) || pi.PropertyType == typeof(Font) || pi.PropertyType == typeof(Color) || pi.PropertyType == typeof(Image))
     {
         return(true);
     }
     return(false);
 }
        private void SerializeProperty(object objectToSerialize, object parent, string elementName, XmlNode xmlParentNode, XmlDocument xmlDocument)
        {
            if (objectToSerialize == null || parent == null)
            {
                return;
            }
            PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(parent)[elementName];

            if (propertyDescriptor != null)
            {
                DefaultValueAttribute defaultValueAttribute = (DefaultValueAttribute)propertyDescriptor.Attributes[typeof(DefaultValueAttribute)];
                if (defaultValueAttribute != null)
                {
                    if (objectToSerialize.Equals(defaultValueAttribute.Value))
                    {
                        return;
                    }
                }
                else
                {
                    MethodInfo method = parent.GetType().GetMethod("ShouldSerialize" + elementName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (method != null)
                    {
                        object obj = method.Invoke(parent, null);
                        if (obj is bool && !(bool)obj)
                        {
                            return;
                        }
                    }
                }
                SerializationVisibilityAttribute serializationVisibilityAttribute = (SerializationVisibilityAttribute)propertyDescriptor.Attributes[typeof(SerializationVisibilityAttribute)];
                if (serializationVisibilityAttribute != null && serializationVisibilityAttribute.Visibility == SerializationVisibility.Hidden)
                {
                    return;
                }
            }
            XmlAttribute xmlAttribute = xmlDocument.CreateAttribute(elementName);

            xmlAttribute.Value = GetXmlValue(objectToSerialize, parent, elementName);
            xmlParentNode.Attributes.Append(xmlAttribute);
        }
        private void SerializeObject(object objectToSerialize, object parent, string elementName, XmlNode xmlParentNode, XmlDocument xmlDocument)
        {
            if (objectToSerialize == null)
            {
                return;
            }
            if (parent != null)
            {
                PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(parent)[elementName];
                if (propertyDescriptor != null)
                {
                    SerializationVisibilityAttribute serializationVisibilityAttribute = (SerializationVisibilityAttribute)propertyDescriptor.Attributes[typeof(SerializationVisibilityAttribute)];
                    if (serializationVisibilityAttribute != null && serializationVisibilityAttribute.Visibility == SerializationVisibility.Hidden)
                    {
                        return;
                    }
                }
            }
            if (objectToSerialize is ICollection)
            {
                SerializeCollection(objectToSerialize, elementName, xmlParentNode, xmlDocument);
                return;
            }
            XmlNode xmlNode = xmlDocument.CreateElement(elementName);

            xmlParentNode.AppendChild(xmlNode);
            bool flag = false;

            if (base.TemplateMode && parent is IList)
            {
                XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("_Template_");
                if (((IList)parent).Count == 1)
                {
                    xmlAttribute.Value = "All";
                }
                else
                {
                    xmlAttribute.Value = ((IList)parent).IndexOf(objectToSerialize).ToString(CultureInfo.InvariantCulture);
                }
                xmlNode.Attributes.Append(xmlAttribute);
                flag = true;
            }
            PropertyInfo[] properties = objectToSerialize.GetType().GetProperties();
            if (properties == null)
            {
                return;
            }
            PropertyInfo[] array = properties;
            foreach (PropertyInfo propertyInfo in array)
            {
                if ((flag && propertyInfo.Name == "Name") || IsGaugeBaseProperty(objectToSerialize, parent, propertyInfo))
                {
                    continue;
                }
                if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("ICollection", ignoreCase: true) != null)
                {
                    bool flag2 = true;
                    if (objectToSerialize != null)
                    {
                        PropertyDescriptor propertyDescriptor2 = TypeDescriptor.GetProperties(objectToSerialize)[propertyInfo.Name];
                        if (propertyDescriptor2 != null)
                        {
                            SerializationVisibilityAttribute serializationVisibilityAttribute2 = (SerializationVisibilityAttribute)propertyDescriptor2.Attributes[typeof(SerializationVisibilityAttribute)];
                            if (serializationVisibilityAttribute2 != null && serializationVisibilityAttribute2.Visibility == SerializationVisibility.Hidden)
                            {
                                flag2 = false;
                            }
                        }
                    }
                    if (flag2)
                    {
                        SerializeCollection(propertyInfo.GetValue(objectToSerialize, null), propertyInfo.Name, xmlNode, xmlDocument);
                    }
                }
                else
                {
                    if (!propertyInfo.CanRead || !propertyInfo.CanWrite || propertyInfo.Name == "Item")
                    {
                        continue;
                    }
                    if (ShouldSerializeAsAttribute(propertyInfo, objectToSerialize))
                    {
                        if (IsSerializableContent(propertyInfo.Name, objectToSerialize))
                        {
                            SerializeProperty(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, xmlNode, xmlDocument);
                        }
                    }
                    else
                    {
                        SerializeObject(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, xmlNode, xmlDocument);
                    }
                }
            }
        }
        private void SerializeObject(object objectToSerialize, object parent, string elementName, BinaryWriter writer)
        {
            if (objectToSerialize == null)
            {
                return;
            }
            if (parent != null)
            {
                PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(parent)[elementName];
                if (propertyDescriptor != null)
                {
                    SerializationVisibilityAttribute serializationVisibilityAttribute = (SerializationVisibilityAttribute)propertyDescriptor.Attributes[typeof(SerializationVisibilityAttribute)];
                    if (serializationVisibilityAttribute != null && serializationVisibilityAttribute.Visibility == SerializationVisibility.Hidden)
                    {
                        return;
                    }
                }
            }
            if (objectToSerialize is ICollection)
            {
                SerializeCollection(objectToSerialize, elementName, writer);
                return;
            }
            writer.Write((short)hashCodeProvider.GetHashCode(elementName));
            long      num       = writer.Seek(0, SeekOrigin.Current);
            ArrayList arrayList = new ArrayList();

            PropertyInfo[] properties = objectToSerialize.GetType().GetProperties();
            if (properties != null)
            {
                PropertyInfo[] array = properties;
                foreach (PropertyInfo propertyInfo in array)
                {
                    if (IsGaugeBaseProperty(objectToSerialize, parent, propertyInfo))
                    {
                        continue;
                    }
                    if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("ICollection", ignoreCase: true) != null)
                    {
                        bool flag = true;
                        if (objectToSerialize != null)
                        {
                            PropertyDescriptor propertyDescriptor2 = TypeDescriptor.GetProperties(objectToSerialize)[propertyInfo.Name];
                            if (propertyDescriptor2 != null)
                            {
                                SerializationVisibilityAttribute serializationVisibilityAttribute2 = (SerializationVisibilityAttribute)propertyDescriptor2.Attributes[typeof(SerializationVisibilityAttribute)];
                                if (serializationVisibilityAttribute2 != null && serializationVisibilityAttribute2.Visibility == SerializationVisibility.Hidden)
                                {
                                    flag = false;
                                }
                            }
                        }
                        if (flag)
                        {
                            arrayList.Add(propertyInfo.Name);
                            SerializeCollection(propertyInfo.GetValue(objectToSerialize, null), propertyInfo.Name, writer);
                        }
                    }
                    else
                    {
                        if (!propertyInfo.CanRead || !propertyInfo.CanWrite || propertyInfo.Name == "Item")
                        {
                            continue;
                        }
                        if (ShouldSerializeAsAttribute(propertyInfo, objectToSerialize))
                        {
                            if (IsSerializableContent(propertyInfo.Name, objectToSerialize))
                            {
                                SerializeProperty(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, writer);
                            }
                        }
                        else
                        {
                            SerializeObject(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, writer);
                        }
                        arrayList.Add(propertyInfo.Name);
                    }
                }
                CheckPropertiesID(arrayList);
            }
            if (writer.Seek(0, SeekOrigin.Current) == num)
            {
                writer.Seek(-2, SeekOrigin.Current);
                writer.Write((short)0);
                writer.Seek(-2, SeekOrigin.Current);
            }
            else
            {
                writer.Write((short)0);
            }
        }
 protected void ResetObjectProperties(object objectToReset, object parent, string elementName)
 {
     if (objectToReset == null)
     {
         return;
     }
     if (objectToReset is IList && IsSerializableContent(elementName, parent))
     {
         ((IList)objectToReset).Clear();
         return;
     }
     PropertyInfo[] properties = objectToReset.GetType().GetProperties();
     if (properties == null)
     {
         return;
     }
     PropertyInfo[] array = properties;
     foreach (PropertyInfo propertyInfo in array)
     {
         PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(objectToReset)[propertyInfo.Name];
         if (propertyDescriptor != null)
         {
             SerializationVisibilityAttribute serializationVisibilityAttribute = (SerializationVisibilityAttribute)propertyDescriptor.Attributes[typeof(SerializationVisibilityAttribute)];
             if (serializationVisibilityAttribute != null && serializationVisibilityAttribute.Visibility == SerializationVisibility.Hidden)
             {
                 continue;
             }
         }
         bool flag = IsSerializableContent(propertyInfo.Name, objectToReset);
         if (IsGaugeBaseProperty(objectToReset, parent, propertyInfo))
         {
             continue;
         }
         if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("IList", ignoreCase: true) != null)
         {
             if (flag)
             {
                 ((IList)propertyInfo.GetValue(objectToReset, null)).Clear();
                 continue;
             }
             foreach (object item in (IList)propertyInfo.GetValue(objectToReset, null))
             {
                 ResetObjectProperties(item, objectToReset, GetObjectName(item));
             }
         }
         else
         {
             if (!propertyInfo.CanRead || !propertyInfo.CanWrite || propertyInfo.Name == "Item")
             {
                 continue;
             }
             if (ShouldSerializeAsAttribute(propertyInfo, objectToReset))
             {
                 if (!flag || propertyDescriptor == null)
                 {
                     continue;
                 }
                 object value = propertyInfo.GetValue(objectToReset, null);
                 DefaultValueAttribute defaultValueAttribute = (DefaultValueAttribute)propertyDescriptor.Attributes[typeof(DefaultValueAttribute)];
                 if (defaultValueAttribute != null)
                 {
                     if (!value.Equals(defaultValueAttribute.Value))
                     {
                         propertyDescriptor.SetValue(objectToReset, defaultValueAttribute.Value);
                     }
                     continue;
                 }
                 MethodInfo method = objectToReset.GetType().GetMethod("Reset" + propertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                 if (method != null)
                 {
                     method.Invoke(objectToReset, null);
                 }
             }
             else
             {
                 ResetObjectProperties(propertyInfo.GetValue(objectToReset, null), objectToReset, propertyInfo.Name);
             }
         }
     }
 }