/// <summary> /// Writes a simple element to the writer. /// </summary> /// <param name="tagName">The name of the tag to write.</param> /// <param name="writer">The XmlWriter to write to.</param> /// <param name="entry">The entry to write to the element.</param> private void WriteValueElement(string tagName, XmlTextWriter writer, CustomSerializationEntry entry) { // write opening tag writer.WriteStartElement(tagName); if (entry.ObjectType.IsEnum) { writer.WriteValue(((int)entry.Value).ToString()); } else { writer.WriteValue(entry.Value); } // write closing tag writer.WriteEndElement(); }
private IEnumerable<CustomSerializationEntry> GetMemberInfo( object objectToSerialize, [NotNull] Type objectToSerializeType, [NotNull] FormatterConverter converter) { ISurrogateSelector selector1; ISerializationSurrogate serializationSurrogate; SerializationInfo info = null; // if the passed object is null, break the iteration. if (objectToSerialize == null) yield break; if ((SurrogateSelector != null) && ((serializationSurrogate = SurrogateSelector.GetSurrogate(objectToSerializeType, Context, out selector1)) != null)) { // use a surrogate to get the members. info = new SerializationInfo(objectToSerializeType, converter); if (!objectToSerializeType.IsPrimitive) // get the data from the surrogate. serializationSurrogate.GetObjectData(objectToSerialize, info, Context); } else { ISerializable serializable = objectToSerialize as ISerializable; if (serializable != null) { // object implements ISerializable if (!objectToSerializeType.IsSerializable) throw new SerializationException( string.Format( // ReSharper disable once AssignNullToNotNullAttribute Resources.XmlFormatter_GetMemberInfo_TypeNotSerializable, objectToSerializeType.FullName)); info = new SerializationInfo(objectToSerializeType, converter); // get the data using ISerializable. serializable.GetObjectData(info, Context); } } if (info != null) // either the surrogate provided the members, or the members were retrieved // using ISerializable. // create the custom entries collection by copying all the members foreach (SerializationEntry member in info) { CustomSerializationEntry entry = new CustomSerializationEntry( // ReSharper disable AssignNullToNotNullAttribute member.Name, member.ObjectType, // ReSharper restore AssignNullToNotNullAttribute false, member.Value); // yield return will return the entry now and return to this point when // the enclosing loop (the one that contains the call to this method) // request the next item. yield return entry; } else { // The item does not have a surrogate, nor does it implement ISerializable. // We use reflection to get the objects state. if (!objectToSerializeType.IsSerializable) throw new SerializationException( string.Format( // ReSharper disable once AssignNullToNotNullAttribute Resources.XmlFormatter_GetMemberInfo_TypeNotSerializable, objectToSerializeType.FullName)); // Get all serializable members MemberInfo[] members = FormatterServices.GetSerializableMembers(objectToSerializeType, Context); Debug.Assert(members != null); foreach (MemberInfo member in members) { Debug.Assert(member != null); if (CanSerialize(member)) { // create the entry CustomSerializationEntry entry = new CustomSerializationEntry( member.Name, // ReSharper disable once AssignNullToNotNullAttribute member.ReflectedType, false); if (typeof(IList).IsAssignableFrom(entry.ObjectType)) entry.IsList = true; // get the value of the member entry.Value = GetMemberValue(objectToSerialize, member); // yield return will return the entry now and return to this point when // the enclosing loop (the one that contains the call to this method) // request the next item. yield return entry; } } } }
/// <summary> /// Writes a simple element to the writer. /// </summary> /// <param name="tagName">The name of the tag to write.</param> /// <param name="writer">The <see cref="System.Xml.XmlTextWriter">XMLTextWriter</see> to write to.</param> /// <param name="entry">The entry to write to the element.</param> private void WriteValueElement( [NotNull] string tagName, [NotNull] XmlTextWriter writer, CustomSerializationEntry entry) { // write opening tag writer.WriteStartElement(tagName); if (entry.Value != null) { if (entry.ObjectType.IsEnum) writer.WriteValue(((int)entry.Value).ToString()); else writer.WriteValue(entry.Value); } // write closing tag writer.WriteEndElement(); }
/// <summary> /// Writes a simple element to the writer. /// The name of the element is the name of the object <see cref="Type"/>. /// </summary> /// <param name="writer">The XMLTextWriter to write to.</param> /// <param name="entry"> /// The <see cref="CustomSerializationEntry">entry</see> to write to the element. /// </param> private void WriteValueElement([NotNull] XmlTextWriter writer, CustomSerializationEntry entry) { WriteValueElement(entry.Name, writer, entry); }
private IEnumerable <CustomSerializationEntry> GetMemberInfo( object objectToSerialize, [NotNull] Type objectToSerializeType, [NotNull] FormatterConverter converter) { ISurrogateSelector selector1; ISerializationSurrogate serializationSurrogate; SerializationInfo info = null; // if the passed object is null, break the iteration. if (objectToSerialize == null) { yield break; } if ((SurrogateSelector != null) && ((serializationSurrogate = SurrogateSelector.GetSurrogate(objectToSerializeType, Context, out selector1)) != null)) { // use a surrogate to get the members. info = new SerializationInfo(objectToSerializeType, converter); if (!objectToSerializeType.IsPrimitive) { // get the data from the surrogate. serializationSurrogate.GetObjectData(objectToSerialize, info, Context); } } else { ISerializable serializable = objectToSerialize as ISerializable; if (serializable != null) { // object implements ISerializable if (!objectToSerializeType.IsSerializable) { throw new SerializationException( string.Format( // ReSharper disable once AssignNullToNotNullAttribute Resources.XmlFormatter_GetMemberInfo_TypeNotSerializable, objectToSerializeType.FullName)); } info = new SerializationInfo(objectToSerializeType, converter); // get the data using ISerializable. serializable.GetObjectData(info, Context); } } if (info != null) { // either the surrogate provided the members, or the members were retrieved // using ISerializable. // create the custom entries collection by copying all the members foreach (SerializationEntry member in info) { CustomSerializationEntry entry = new CustomSerializationEntry( // ReSharper disable AssignNullToNotNullAttribute member.Name, member.ObjectType, // ReSharper restore AssignNullToNotNullAttribute false, member.Value); // yield return will return the entry now and return to this point when // the enclosing loop (the one that contains the call to this method) // request the next item. yield return(entry); } } else { // The item does not have a surrogate, nor does it implement ISerializable. // We use reflection to get the objects state. if (!objectToSerializeType.IsSerializable) { throw new SerializationException( string.Format( // ReSharper disable once AssignNullToNotNullAttribute Resources.XmlFormatter_GetMemberInfo_TypeNotSerializable, objectToSerializeType.FullName)); } // Get all serializable members MemberInfo[] members = FormatterServices.GetSerializableMembers(objectToSerializeType, Context); Debug.Assert(members != null); foreach (MemberInfo member in members) { Debug.Assert(member != null); if (CanSerialize(member)) { // create the entry CustomSerializationEntry entry = new CustomSerializationEntry( member.Name, // ReSharper disable once AssignNullToNotNullAttribute member.ReflectedType, false); if (typeof(IList).IsAssignableFrom(entry.ObjectType)) { entry.IsList = true; } // get the value of the member entry.Value = GetMemberValue(objectToSerialize, member); // yield return will return the entry now and return to this point when // the enclosing loop (the one that contains the call to this method) // request the next item. yield return(entry); } } } }
/// <summary> /// Writes a simple element to the writer. The name of the element is the name of the object type. /// </summary> /// <param name="writer">The XmlWriter to write to.</param> /// <param name="entry">The entry to write to the element.</param> private void WriteValueElement(XmlWriter writer, CustomSerializationEntry entry) { WriteValueElement(entry.Name, writer, entry); }