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(CultureInfo) && pi.PropertyType != typeof(Color) && pi.PropertyType != typeof(Image) && pi.PropertyType != typeof(ShapeData) && pi.PropertyType != typeof(Stream) && pi.PropertyType != typeof(Type) && pi.PropertyType != typeof(DateTime) && pi.PropertyType != typeof(Margins) && pi.PropertyType != typeof(MapCoordinate)) { return(false); } return(true); }
private void SerializeProperty(object objectToSerialize, object parent, string elementName, XmlNode xmlParentNode, XmlDocument xmlDocument) { if (objectToSerialize != null && parent != null) { 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 = this.GetXmlValue(objectToSerialize, parent, elementName); xmlParentNode.Attributes.Append(xmlAttribute); } }
private void SerializeObject(object objectToSerialize, object parent, string elementName, XmlNode xmlParentNode, XmlDocument xmlDocument) { if (objectToSerialize != null) { 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) { this.SerializeCollection(objectToSerialize, elementName, xmlParentNode, xmlDocument); } else { 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.CurrentCulture); } xmlNode.Attributes.Append(xmlAttribute); flag = true; } PropertyInfo[] properties = objectToSerialize.GetType().GetProperties(); if (properties != null) { PropertyInfo[] array = properties; foreach (PropertyInfo propertyInfo in array) { if ((!flag || !(propertyInfo.Name == "Name")) && !base.IsMapBaseProperty(objectToSerialize, parent, propertyInfo)) { if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("ICollection", 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) { this.SerializeCollection(propertyInfo.GetValue(objectToSerialize, null), propertyInfo.Name, xmlNode, xmlDocument); } } else if (propertyInfo.CanRead && propertyInfo.CanWrite && !(propertyInfo.Name == "Item") && !(propertyInfo.Name == "Tag")) { if (base.ShouldSerializeAsAttribute(propertyInfo, objectToSerialize)) { if (base.IsSerializableContent(propertyInfo.Name, objectToSerialize)) { this.SerializeProperty(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, xmlNode, xmlDocument); } } else { this.SerializeObject(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, xmlNode, xmlDocument); } } } } } } } }
protected void ResetObjectProperties(object objectToReset, object parent, string elementName) { if (objectToReset != null) { if (objectToReset is IList && this.IsSerializableContent(elementName, parent)) { ((IList)objectToReset).Clear(); } else { PropertyInfo[] properties = objectToReset.GetType().GetProperties(); if (properties != null) { 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 = this.IsSerializableContent(propertyInfo.Name, objectToReset); if (!this.IsMapBaseProperty(objectToReset, parent, propertyInfo)) { if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("IList", true) != null) { if (flag) { ((IList)propertyInfo.GetValue(objectToReset, null)).Clear(); } else { foreach (object item in (IList)propertyInfo.GetValue(objectToReset, null)) { this.ResetObjectProperties(item, objectToReset, this.GetObjectName(item)); } } } else if (propertyInfo.CanRead && propertyInfo.CanWrite && !(propertyInfo.Name == "Item")) { if (this.ShouldSerializeAsAttribute(propertyInfo, objectToReset)) { if (flag && propertyDescriptor != null) { 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); } } else { MethodInfo method = objectToReset.GetType().GetMethod("Reset" + propertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (method != null) { method.Invoke(objectToReset, null); } } } } else { this.ResetObjectProperties(propertyInfo.GetValue(objectToReset, null), objectToReset, propertyInfo.Name); } } } } } } } }
private void SerializeObject(object objectToSerialize, object parent, string elementName, BinaryWriter writer) { if (objectToSerialize != null) { 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) { this.SerializeCollection(objectToSerialize, elementName, writer); } else { writer.Write((short)this.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 (!base.IsMapBaseProperty(objectToSerialize, parent, propertyInfo)) { if (propertyInfo.CanRead && propertyInfo.PropertyType.GetInterface("ICollection", 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); this.SerializeCollection(propertyInfo.GetValue(objectToSerialize, null), propertyInfo.Name, writer); } } else if (propertyInfo.CanRead && propertyInfo.CanWrite && !(propertyInfo.Name == "Item")) { if (base.ShouldSerializeAsAttribute(propertyInfo, objectToSerialize)) { if (base.IsSerializableContent(propertyInfo.Name, objectToSerialize)) { this.SerializeProperty(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, writer); } } else { this.SerializeObject(propertyInfo.GetValue(objectToSerialize, null), objectToSerialize, propertyInfo.Name, writer); } arrayList.Add(propertyInfo.Name); } } } this.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); } } } }