/* * Add */ /// <summary> /// Adds the specified <see cref="T:NuGenReference"/> to this <see cref="T:NuGenReferenceCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="T:NuGenPropertyInfo"/> that is used to /// initialize a <see cref="T:NuGenReference"/> instance to add.</param> /// <param name="standardPropertyInfo">Specifies the <see cref="T:PropertyInfo"/> that is used to /// initialize a <see cref="T:NuGenReference"/> instance to add.</param> /// <param name="obj">Specifies the <see cref="T:Object"/> that is used to initialize a <see cref="T:NuGenReference"/> to add.</param> public void Add( NuGenPropertyInfo propertyInfo, PropertyInfo standardPropertyInfo, object obj ) { this.List.Add(new NuGenReference(propertyInfo, standardPropertyInfo, obj)); }
/// <summary> /// Initializes a new instance of the <see cref="NuGenReference"/> class. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="T:NuGenPropertyInfo"/> of the object being /// serialized as a reference.</param> /// <param name="standardPropertyInfo">Specifies the <see cref="T:PropertyInfo"/> of the object being /// serialized as a reference.</param> /// <param name="obj">Specifies the object being serialized as a reference.</param> public NuGenReference( NuGenPropertyInfo propertyInfo, PropertyInfo standardPropertyInfo, object obj ) { this.PropertyInfo = propertyInfo; this.StandardPropertyInfo = standardPropertyInfo; this.Object = obj; }
/* * Add */ /// <summary> /// Adds the specified <see cref="NuGenPropertyInfo"/> to this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="NuGenPropertyInfo"/> to add.</param> /// <exception cref="ArgumentNullException"><paramref name="propertyInfo"/> is <see langword="null"/>.</exception> public void Add(NuGenPropertyInfo propertyInfo) { if (propertyInfo == null) { throw new ArgumentNullException("propertyInfo"); } propertyInfo.Parent = _parent; this.List.Add(propertyInfo); }
/* * Remove */ /// <summary> /// Removes the specified <see cref="NuGenPropertyInfo"/> from this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="NuGenPropertyInfo"/> to remove.</param> public void Remove(NuGenPropertyInfo propertyInfo) { this.List.Remove(propertyInfo); }
/* * Insert */ /// <summary> /// Inserts the <see cref="NuGenPropertyInfo"/> at the specified index into this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="index">Specifes the zero-based index at which the <see cref="T:NuGenPropertyInfo"/> should be inserted.</param> /// <param name="propertyInfo">Specifies the <see cref="T:NuGenPropertyInfo"/> to be inserted.</param> public void Insert(int index, NuGenPropertyInfo propertyInfo) { this.List.Insert(index, propertyInfo); }
/* * Contains */ /// <summary> /// Determines whether the specified <see cref="NuGenPropertyInfo"/> is in this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="NuGenPropertyInfo"/> to locate in this /// <see cref="NuGenPropertyInfoCollection"/>. The value can be <see langword="null"/>.</param> public bool Contains(NuGenPropertyInfo propertyInfo) { return this.List.Contains(propertyInfo); }
/// <summary> /// Serializes the specified list. /// </summary> /// <param name="properties">Specifies the <see cref="NuGenPropertyInfoCollection"/> to be filled /// with the elements of the specified list.</param> /// <param name="list">Specifies the list to serialize.</param> /// <returns>Returns the number of elements in the specified list.</returns> private int SerializeList(NuGenPropertyInfoCollection properties, object list) { if (list == null) { return 0; } Debug.Assert(properties != null, "properties != null"); Debug.Assert(list is IList, "list is IList"); int count = 0; foreach (object item in (IList)list) { this.OnSerializing(EventArgs.Empty); if ( (item.GetType().IsPrimitive) || (item is string) || (this.IsContent(item)) ) { properties.Add( new NuGenPropertyInfo(Resources.XmlAttribute_Value, item, false, false, false) ); } else { int index = _graph[item]; if (index == -1) { _graph.Add(item); NuGenPropertyInfo propertyInfo = null; if (this.CheckSerializable && (item is INuGenSerializable)) { propertyInfo = new NuGenPropertyInfo( string.Format(CultureInfo.InvariantCulture, "{0}{1}", Resources.XmlTag_Item, (count + 1).ToString(CultureInfo.InvariantCulture)), item, true, false, false ); propertyInfo.IsSerializable = true; } else { string propertyName = Resources.XmlTag_Item; PropertyInfo namePropertyInfo = item.GetType().GetProperty("Name"); if (namePropertyInfo != null) { string namePropertyValue = (string)namePropertyInfo.GetValue(item, null); if (!string.IsNullOrEmpty(namePropertyValue)) { propertyName = namePropertyValue; } } propertyInfo = new NuGenPropertyInfo( string.Format(CultureInfo.InvariantCulture, "{0}{1}", propertyName, (count + 1).ToString(CultureInfo.InvariantCulture)), item, true, false, false ); propertyInfo.Properties.AddRange(this.SerializeObject(item)); } Debug.Assert(propertyInfo != null, "propertyInfo != null"); propertyInfo.ReferenceCode = _graph[item]; properties.Add(propertyInfo); } else { NuGenPropertyInfo propertyInfo = new NuGenPropertyInfo( Resources.XmlTag_Item, item, true, true, false ); propertyInfo.ReferenceCode = index; properties.Add(propertyInfo); } } count++; } return count; }
/// <summary> /// Initializes a new instance of the <see cref="NuGenPropertyInfoCollection"/> class. /// </summary> /// <param name="parent">Specifies the parent for this <see cref="T:NuGenPropertyInfoCollection"/>.</param> public NuGenPropertyInfoCollection(NuGenPropertyInfo parent) { _parent = parent; }
/* * IndexOf */ /// <summary> /// Searches for the specified <see cref="NuGenPropertyInfo"/> and returns the zero-based index of /// the first occurrence in this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="NuGenPropertyInfo"/> to locate in this <see cref="NuGenPropertyInfoCollection"/>.</param> public int IndexOf(NuGenPropertyInfo propertyInfo) { return this.List.IndexOf(propertyInfo); }
/// <summary> /// Serializes the specified <see cref="Object"/>. Can be <see langword="null"/>. /// </summary> /// <param name="obj">Specifies the <see cref="Object"/> to serialize.</param> /// <returns></returns> public NuGenPropertyInfoCollection SerializeObject(object obj) { if (obj == null) { return null; } if (_graph[obj] == -1) { _graph.Add(obj); } NuGenPropertyInfoCollection properties = new NuGenPropertyInfoCollection(); PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(obj); /* If no public properties found return null. */ if ((descriptors == null) || (descriptors.Count == 0)) { return null; } if (this.SortProperties) { descriptors = descriptors.Sort(); } /* * Serialize public properties. */ for (int i = 0; i < descriptors.Count; i++) { this.OnSerializing(EventArgs.Empty); PropertyDescriptor descriptor = descriptors[i]; NuGenSerializationVisibilityAttribute attribute = (NuGenSerializationVisibilityAttribute)descriptor.Attributes[typeof(NuGenSerializationVisibilityAttribute)]; /* This is the default value for the property that is not marked with * NuGenSerializationVisibilityAttribute attribute. */ NuGenSerializationVisibility visibility = NuGenSerializationVisibility.Content; object propertyDefaultValue = NuGenSerializer.GetDefaultValue(descriptor); string propertyName = descriptor.Name; object propertyValue = descriptor.GetValue(obj); /* If the property is marked with the NuGenSerializationVisibilityAttribute attribute... */ if (attribute != null) { /* Skip properties that are not visible for the serializer. */ if (attribute.Visibility == NuGenSerializationVisibility.Hidden) { continue; } visibility = attribute.Visibility; } /* If the property is not marked with the NuGenSerializationVisibilityAttribute attribute... */ else { if ( (descriptor.SerializationVisibility == DesignerSerializationVisibility.Content) && !(propertyValue is IList) && !(descriptor.PropertyType.IsEnum) ) { visibility = NuGenSerializationVisibility.Reference; } else if ( !(descriptor.ShouldSerializeValue(obj)) || (descriptor.SerializationVisibility == DesignerSerializationVisibility.Hidden) ) { continue; } } NuGenPropertyInfo propertyInfo = null; if (propertyValue == null) { propertyInfo = new NuGenPropertyInfo( propertyName, propertyValue, propertyDefaultValue, true, false, false ); } else { switch (visibility) { case NuGenSerializationVisibility.Reference: { propertyInfo = new NuGenPropertyInfo( propertyName, propertyValue, propertyDefaultValue, true, true, false ); _references.Add(propertyInfo); break; } case NuGenSerializationVisibility.Content: { /* * Automatically determine if the marked property is a collection. */ if (propertyValue is IList) { propertyInfo = new NuGenPropertyInfo( propertyName, propertyValue, propertyDefaultValue, false, false, true ); propertyInfo.Count = this.SerializeList( propertyInfo.Properties, propertyValue ); } else { propertyInfo = new NuGenPropertyInfo( propertyName, propertyValue, propertyDefaultValue, false, false, false ); } break; } case NuGenSerializationVisibility.Class: { if (!propertyValue.GetType().IsClass) { throw new InvalidCastException( string.Format(CultureInfo.InvariantCulture, Properties.Resources.InvalidCast_NotClass, propertyName) ); } NuGenReferenceIgnoreAttribute[] ignoreAttributes = (NuGenReferenceIgnoreAttribute[])descriptor.PropertyType.GetCustomAttributes(typeof(NuGenReferenceIgnoreAttribute), false); int valueInitial = _graph[propertyValue]; if ((valueInitial == -1) || (ignoreAttributes.Length > 0)) { _graph.Add(propertyValue); int valueAfterAdd = _graph[propertyValue]; if ( (this.CheckSerializable) && (propertyValue is INuGenSerializable) ) { propertyInfo = new NuGenPropertyInfo( propertyName, propertyValue, propertyDefaultValue, true, false, false ); propertyInfo.IsSerializable = true; } else { propertyInfo = new NuGenPropertyInfo( propertyName, propertyValue, propertyDefaultValue, true, false, false ); NuGenPropertyInfoCollection innerProperties = this.SerializeObject(propertyValue); if (innerProperties != null) { propertyInfo.Properties.AddRange(innerProperties); } } propertyInfo.ReferenceCode = valueAfterAdd; } else { propertyInfo = new NuGenPropertyInfo( propertyName, propertyValue, propertyDefaultValue, true, true, false ); propertyInfo.ReferenceCode = valueInitial; } break; } } } if (propertyInfo != null) { properties.Add(propertyInfo); } } return properties; }
private void SerializeProperty(XmlTextWriter xmlTextWriter, NuGenPropertyInfo propertyInfo) { string propertyName = XmlConvert.EncodeName( NuGenSerializer.GetStringFromProperty(propertyInfo.Name) ); if (propertyInfo.Value == null) { if (propertyInfo.DefaultValue != null) { xmlTextWriter.WriteStartElement(propertyName); xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_IsNull, Resources.XmlValue_True); xmlTextWriter.WriteEndElement(); } } else if (propertyInfo.IsKey) { if (propertyInfo.IsReference) { xmlTextWriter.WriteStartElement(propertyName); xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_IsRef, propertyInfo.ReferenceCode.ToString(CultureInfo.InvariantCulture)); xmlTextWriter.WriteEndElement(); } else { xmlTextWriter.WriteStartElement(propertyName); if (propertyInfo.ReferenceCode != -1) { xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_Ref, propertyInfo.ReferenceCode.ToString(CultureInfo.InvariantCulture)); } if (propertyInfo.Value != null) { string typeString = (string)NuGenSerializer.TypeToString[propertyInfo.Value.GetType()]; if (typeString == null) { typeString = propertyInfo.Value.GetType().FullName; } xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_Type, typeString); } if (propertyInfo.IsSerializable) { xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_IsSer, Resources.XmlValue_True); INuGenSerializable serializable = (INuGenSerializable)propertyInfo.Value; serializable.Serialize(_converter, xmlTextWriter); _graph.Add(serializable, propertyInfo.ReferenceCode); } else { xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_IsKey, Resources.XmlValue_True); this.SerializeObject(xmlTextWriter, propertyInfo.Properties); } xmlTextWriter.WriteEndElement(); } } else if (propertyInfo.IsList) { xmlTextWriter.WriteStartElement(propertyName); xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_IsList, Resources.XmlValue_True); if (propertyInfo.Value != null) { xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_Count, propertyInfo.Count.ToString(CultureInfo.InvariantCulture)); } this.SerializeObject(xmlTextWriter, propertyInfo.Properties); xmlTextWriter.WriteEndElement(); } else if (propertyInfo.Value is Metafile) { xmlTextWriter.WriteStartElement(propertyName); Metafile metafile = (Metafile)propertyInfo.Value; byte[] buffer = NuGenMetafileConverter.MetafileToBytes(metafile); xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_IsEmfImage, Resources.XmlValue_True); xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_Length, buffer.Length.ToString(CultureInfo.InvariantCulture)); xmlTextWriter.WriteBase64(buffer, 0, buffer.Length); xmlTextWriter.WriteEndElement(); } else if (propertyInfo.Value is Image) { xmlTextWriter.WriteStartElement(propertyName); Image image = (Image)propertyInfo.Value; byte[] buffer = NuGenImageConverter.ImageToBytes(image); xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_IsImage, Resources.XmlValue_True); xmlTextWriter.WriteAttributeString(Resources.XmlAttribute_Length, buffer.Length.ToString(CultureInfo.InvariantCulture)); xmlTextWriter.WriteBase64(buffer, 0, buffer.Length); xmlTextWriter.WriteEndElement(); } else { string objectString = _converter.ObjectToString(propertyInfo.Value); if (objectString != null) { xmlTextWriter.WriteStartElement(propertyName); xmlTextWriter.WriteString(objectString); xmlTextWriter.WriteEndElement(); } } }
/* * Add */ /// <summary> /// Adds the specified <see cref="T:NuGenReference"/> to this <see cref="T:NuGenReferenceCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="T:NuGenPropertyInfo"/> that is used to /// initialize a <see cref="T:NuGenReference"/> instance to add.</param> public void Add(NuGenPropertyInfo propertyInfo) { this.List.Add(new NuGenReference(propertyInfo)); }
private NuGenPropertyInfoCollection DeserializeObject(XmlTextReader xmlTextReader) { Debug.Assert(xmlTextReader != null, "xmlTextReader != null"); NuGenPropertyInfoCollection propertyInfoCollection = new NuGenPropertyInfoCollection(); xmlTextReader.Read(); while (!xmlTextReader.EOF) { if (xmlTextReader.IsStartElement()) { this.OnDeserializing(EventArgs.Empty); string propertyString = xmlTextReader.Name; bool isKey = xmlTextReader.GetAttribute(Resources.XmlAttribute_IsKey) == Resources.XmlValue_True; bool isRef = xmlTextReader.MoveToAttribute(Resources.XmlAttribute_IsRef); bool isList = xmlTextReader.MoveToAttribute(Resources.XmlAttribute_IsList); NuGenPropertyInfo propertyInfo = new NuGenPropertyInfo( XmlConvert.DecodeName(NuGenSerializer.GetPropertyFromString(propertyString)), null, isKey, isRef, isList ); if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsNull) == Resources.XmlValue_True) { propertyInfo.Value = null; } else if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsSer) != null) { string refAttrValue = xmlTextReader.GetAttribute(Resources.XmlAttribute_Ref); if (refAttrValue != null) { propertyInfo.ReferenceCode = int.Parse(refAttrValue, CultureInfo.InvariantCulture); } string typeAttrValue = xmlTextReader.GetAttribute(Resources.XmlAttribute_Type); Type typeFromAttr = NuGenSerializer.StringToType[typeAttrValue] as Type; if (typeFromAttr == null) { propertyInfo.Value = NuGenActivator.CreateObject(typeAttrValue); } else { propertyInfo.Value = NuGenActivator.CreateObject(typeFromAttr); } propertyInfo.IsSerializable = true; INuGenSerializable serializable = (INuGenSerializable)propertyInfo.Value; serializable.Deserialize(_converter, xmlTextReader); _graph.Add(serializable, propertyInfo.ReferenceCode); } else if (propertyInfo.IsReference) { propertyInfo.ReferenceCode = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_IsRef), CultureInfo.InvariantCulture); } else if (propertyInfo.IsList) { propertyInfo.Count = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_Count), CultureInfo.InvariantCulture); if ((propertyInfo.Count > 0) && !xmlTextReader.IsEmptyElement) { propertyInfo.Properties = this.DeserializeObject(xmlTextReader); } } else if (!propertyInfo.IsKey) { if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsImage) == Resources.XmlValue_True) { int bufferLength = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_Length), CultureInfo.InvariantCulture); byte[] buffer = new byte[bufferLength]; xmlTextReader.ReadBase64(buffer, 0, bufferLength); propertyInfo.Value = NuGenImageConverter.BytesToImage(buffer); } else if (xmlTextReader.GetAttribute(Resources.XmlAttribute_IsEmfImage) == Resources.XmlValue_True) { int bufferLength = int.Parse(xmlTextReader.GetAttribute(Resources.XmlAttribute_Length), CultureInfo.InvariantCulture); byte[] buffer = new byte[bufferLength]; xmlTextReader.ReadBase64(buffer, 0, bufferLength); propertyInfo.Value = NuGenMetafileConverter.BytesToMetafile(buffer); } else { propertyInfo.Value = xmlTextReader.ReadString(); } } else if (propertyInfo.IsKey) { string attrRef = xmlTextReader.GetAttribute(Resources.XmlAttribute_Ref); if (attrRef != null) { propertyInfo.ReferenceCode = int.Parse(attrRef, CultureInfo.InvariantCulture); } string typeAttr = xmlTextReader.GetAttribute(Resources.XmlAttribute_Type); Type typeFromAttr = NuGenSerializer.StringToType[typeAttr] as Type; if (typeFromAttr == null) { propertyInfo.Value = NuGenActivator.CreateObject(typeAttr); } else { propertyInfo.Value = NuGenActivator.CreateObject(typeFromAttr); } if (!xmlTextReader.IsEmptyElement) { propertyInfo.Properties = this.DeserializeObject(xmlTextReader); } } propertyInfoCollection.Add(propertyInfo); } else if (xmlTextReader.NodeType == XmlNodeType.EndElement) { return propertyInfoCollection; } xmlTextReader.Read(); } return propertyInfoCollection; }
/* * Contains */ /// <summary> /// Determines whether the specified <see cref="NuGenPropertyInfo"/> is in this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="NuGenPropertyInfo"/> to locate in this /// <see cref="NuGenPropertyInfoCollection"/>. The value can be <see langword="null"/>.</param> public bool Contains(NuGenPropertyInfo propertyInfo) { return(this.List.Contains(propertyInfo)); }
/// <summary> /// Initializes a new instance of the <see cref="NuGenReference"/> class. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="T:NuGenPropertyInfo"/> of the object being /// serialized as a reference.</param> public NuGenReference( NuGenPropertyInfo propertyInfo ) : this(propertyInfo, null, null) { }
/* * IndexOf */ /// <summary> /// Searches for the specified <see cref="NuGenPropertyInfo"/> and returns the zero-based index of /// the first occurrence in this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="propertyInfo">Specifies the <see cref="NuGenPropertyInfo"/> to locate in this <see cref="NuGenPropertyInfoCollection"/>.</param> public int IndexOf(NuGenPropertyInfo propertyInfo) { return(this.List.IndexOf(propertyInfo)); }
/* * AddRange */ /// <summary> /// Adds the specified <see cref="NuGenPropertyInfo"/> array to this <see cref="NuGenPropertyInfoCollection"/>. /// </summary> /// <param name="propertyInfoCollection">Specifies the <see cref="NuGenPropertyInfo"/> array to add.</param> /// <exception cref="ArgumentNullException"><paramref name="propertyInfoCollection"/> is <see langword="null"/>.</exception> public void AddRange(NuGenPropertyInfo[] propertyInfoCollection) { if (propertyInfoCollection == null) { throw new ArgumentNullException("propertyInfoCollection"); } foreach (NuGenPropertyInfo propertyInfo in propertyInfoCollection) { this.Add(propertyInfo); } }