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);
 }
 protected bool SerializeICollAsAtribute(PropertyInfo pi, object objectToSerialize)
 {
     if (objectToSerialize != null)
     {
         PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(objectToSerialize)[pi.Name];
         if (propertyDescriptor != null)
         {
             SerializationVisibilityAttribute serializationVisibilityAttribute = (SerializationVisibilityAttribute)propertyDescriptor.Attributes[typeof(SerializationVisibilityAttribute)];
             if (serializationVisibilityAttribute != null && serializationVisibilityAttribute.Visibility == SerializationVisibility.Attribute)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        protected virtual 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);
                    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);
        }
예제 #4
0
        protected virtual void SerializeProperty(object objectToSerialize, object parent, string elementName, BinaryWriter writer)
        {
            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 && objectToSerialize.Equals(defaultValueAttribute.Value))
                {
                    return;
                }
                SerializationVisibilityAttribute serializationVisibilityAttribute = (SerializationVisibilityAttribute)propertyDescriptor.Attributes[typeof(SerializationVisibilityAttribute)];
                if (serializationVisibilityAttribute != null && serializationVisibilityAttribute.Visibility == SerializationVisibility.Hidden)
                {
                    return;
                }
            }
            WritePropertyValue(objectToSerialize, parent, elementName, writer);
        }
 protected virtual 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 (IsChartBaseProperty(objectToReset, parent, propertyInfo))
         {
             continue;
         }
         if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("IList", ignoreCase: true) != null)
         {
             if (flag)
             {
                 bool       flag2  = false;
                 MethodInfo method = objectToReset.GetType().GetMethod("Reset" + propertyInfo.Name);
                 if (method != null)
                 {
                     method.Invoke(objectToReset, null);
                     flag2 = true;
                 }
                 if (!flag2)
                 {
                     ((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 == null)
                     {
                         if (defaultValueAttribute.Value != null)
                         {
                             propertyDescriptor.SetValue(objectToReset, defaultValueAttribute.Value);
                         }
                     }
                     else if (!value.Equals(defaultValueAttribute.Value))
                     {
                         propertyDescriptor.SetValue(objectToReset, defaultValueAttribute.Value);
                     }
                 }
                 else
                 {
                     MethodInfo method2 = objectToReset.GetType().GetMethod("Reset" + propertyInfo.Name);
                     if (method2 != null)
                     {
                         method2.Invoke(objectToReset, null);
                     }
                 }
             }
             else
             {
                 ResetObjectProperties(propertyInfo.GetValue(objectToReset, null), objectToReset, propertyInfo.Name);
             }
         }
     }
 }
예제 #6
0
        protected virtual 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(SerializerBase.GetStringHashCode(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 (IsChartBaseProperty(objectToSerialize, parent, propertyInfo))
                    {
                        continue;
                    }
                    if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("ICollection", ignoreCase: true) != null && !SerializeICollAsAtribute(propertyInfo, objectToSerialize))
                    {
                        bool flag = IsSerializableContent(propertyInfo.Name, objectToSerialize);
                        if (flag && 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;
                                }
                            }
                        }
                        MethodInfo method = objectToSerialize.GetType().GetMethod("ShouldSerialize" + propertyInfo.Name);
                        if (flag && method != null)
                        {
                            object obj = method.Invoke(objectToSerialize, null);
                            if (obj is bool && !(bool)obj)
                            {
                                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;
                        }
                        bool       flag2   = true;
                        MethodInfo method2 = objectToSerialize.GetType().GetMethod("ShouldSerialize" + propertyInfo.Name);
                        if (method2 != null)
                        {
                            object obj2 = method2.Invoke(objectToSerialize, null);
                            if (obj2 is bool && !(bool)obj2)
                            {
                                flag2 = false;
                            }
                        }
                        if (flag2)
                        {
                            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 virtual 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") || IsChartBaseProperty(objectToSerialize, parent, propertyInfo))
                {
                    continue;
                }
                if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("ICollection", ignoreCase: true) != null && !SerializeICollAsAtribute(propertyInfo, objectToSerialize))
                {
                    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;
                            }
                        }
                    }
                    MethodInfo method = objectToSerialize.GetType().GetMethod("ShouldSerialize" + propertyInfo.Name);
                    if (method != null)
                    {
                        object obj = method.Invoke(objectToSerialize, null);
                        if (obj is bool && !(bool)obj)
                        {
                            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);
                    }
                }
            }
        }