public Boolean TryGetCurrentMember(out XmlMemberInfo memberInfo) { if (this.XmlReader.ReadState != ReadState.Interactive) { throw new InvalidOperationException(); } if (this.XmlReader.NodeType != XmlNodeType.Element) { throw new InvalidOperationException(); } Contract.Ensures( (Contract.Result <Boolean>() && Contract.ValueAtReturn(out memberInfo) != null) || (!Contract.Result <Boolean>() && Contract.ValueAtReturn(out memberInfo) == null)); for (Int32 i = 0; i < this.Serializer.SerializableMembers.Count; i++) { XmlSerializableMember member = this.Serializer.SerializableMembers[i]; Type memberType; if (member.MemberInfo.TryGetMemberTypeByName(this.XmlReader.LocalName, out memberType)) { memberInfo = member.MemberInfo; return(true); } } memberInfo = null; return(false); }
/// <exception cref="XmlSerializationException"> /// The member contains a derived type which is not explicitly defined by an XmlTypeDefAttribute which is required by the /// current serialization settings. /// </exception> private void WriteMember( XmlSerializableMember member, Object memberValue, Boolean asAttribute, IEnumerable <XmlItemDefAttribute> itemAttributeOverrides = null ) { XmlItemDefAttributeCollection itemDefAttributes = new XmlItemDefAttributeCollection(member.MemberInfo.ItemDefAttributes); if (itemAttributeOverrides != null) { itemDefAttributes.AddRange(itemAttributeOverrides); } String nameToWrite; if (memberValue != null) { Type objectType = memberValue.GetType(); Boolean typeIsExplicitlyDefined; nameToWrite = member.MemberInfo.GetMemberNameByType(objectType, out typeIsExplicitlyDefined); if (!typeIsExplicitlyDefined && this.Settings.RequiresExplicitTypeDefinition) { var ex = new XmlSerializationException("The member contains a derived type which is not explicitly defined by an XmlTypeDefAttribute which is required by the current serialization settings."); ex.Data.Add("MemberInfo", member.MemberInfo); ex.Data.Add("Derived Type", objectType); throw ex; } } else { nameToWrite = member.MemberInfo.Name; } this.WriteNode(nameToWrite, memberValue, asAttribute, itemDefAttributes, member.MemberInfo.Comment); }
public void WriteRoot(Object instance, String customName = null, Boolean ignoreAttributes = false) { if (instance == null) { throw new ArgumentNullException(); } if (this.AsAttribute) { throw new InvalidOperationException(); } Contract.Requires <InvalidOperationException>( this.XmlWriter.WriteState == WriteState.Content || this.XmlWriter.WriteState == WriteState.Element || this.XmlWriter.WriteState == WriteState.Start ); String rootName = customName; if (rootName == null) { rootName = this.ElementName; } XmlRootAttribute xmlRootAttribute = (this.Serializer.RootAttribute as XmlRootAttribute); if (xmlRootAttribute.Comment != null) { this.XmlWriter.WriteComment(xmlRootAttribute.Comment); } if (this.XmlWriter.WriteState == WriteState.Start) { this.XmlWriter.WriteStartDocument(); } this.XmlWriter.WriteStartElement(rootName, xmlRootAttribute.XmlNamespace); if (!ignoreAttributes) { for (Int32 i = 0; i < this.Serializer.SerializableMembers.Count; i++) { XmlSerializableMember member = this.Serializer.SerializableMembers[i]; if (member.MemberInfo.IsAttribute) { this.WriteMember(member, member.GetValue(instance), true); } } } }
/// <exception cref="XmlSerializationException"> /// Root element name does not match the name defined by the metadata. /// </exception> public void ReadRoot( Object instance, String customName = null, Boolean ignoreAttributes = false ) { if (instance == null) { throw new ArgumentNullException(); } if (!this.AsAttribute) { throw new InvalidOperationException(); } Contract.Requires <InvalidOperationException>( this.XmlReader.NodeType == XmlNodeType.None || this.XmlReader.NodeType == XmlNodeType.Element ); this.ReadToRoot(customName); if (ignoreAttributes) { while (this.XmlReader.MoveToNextAttribute()) { } } else { while (this.XmlReader.MoveToNextAttribute()) { for (Int32 i = 0; i < this.Serializer.SerializableMembers.Count; i++) { XmlSerializableMember member = this.Serializer.SerializableMembers[i]; if (member.MemberInfo.IsAttribute) { Type memberType; if (member.MemberInfo.TryGetMemberTypeByName(this.XmlReader.LocalName, out memberType)) { member.SetValue(instance, this.ReadAttributeObject(memberType)); } } } } } }
/// <exception cref="ArgumentException"> /// The given type implements the <see cref="IXmlCustomSerialized" /> interface but does not define a constructor accepting /// <see cref="XmlDeserializationProvider" /> as parameter. /// </exception> private void ReflectTypeData(IEnumerable <Type> typesToCache = null) { if (this.TargetType == null) { throw new InvalidOperationException(); } this.isCustomSerialized = (this.TargetType.GetInterface("IXmlCustomSerialized", false) != null); if (this.IsCustomSerialized) { if (this.TargetType.GetConstructor(new[] { typeof(XmlDeserializationProvider) }) == null) { var ex = new XmlSerializationException("The type implements the IXmlCustomSerialized interface but does not define a constructor accepting XmlDeserializationProvider as parameter."); ex.Data.Add("Type Name", this.TargetType.FullName); throw ex; } } else if (!this.TargetType.IsValueType) // A value type will always provide a parameterless constructor. { if (this.Settings.AllowsDeserialization && this.TargetType.GetConstructor(new Type[] {}) == null) { var ex = new XmlSerializationException("The type has to implement a parameterless constructor to be deserialized."); ex.Data.Add("Type Name", this.TargetType.FullName); throw ex; } } // The types where reflection data has to be cached (where sub serializers have to be created) for. List <Type> typesToCacheNew = new List <Type>(20); if (typesToCache != null) { foreach (Type typeToCache in typesToCache) { typesToCacheNew.Add(typeToCache); } } // Reflect Members MemberInfo[] memberInfos = this.TargetType.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); this.serializableMembers = new XmlSerializableMemberCollection(); foreach (MemberInfo memberInfo in memberInfos) { XmlSerializableMember member = XmlSerializableMember.Create(memberInfo, typesToCacheNew, this.Settings); if (member != null) { this.serializableMembers.Add(member); } } // Sort the members by their OrderIndex value. this.serializableMembers.Sort(); for (Int32 i = 0; i < typesToCacheNew.Count; i++) { Type typeToCache = typesToCacheNew[i]; if ( typeToCache != this.TargetType && typeToCache != null && !this.subSerializers.ContainsKey(typeToCache) && !XmlSerializationProvider.IsNativelySerializable(typeToCache) ) { try { this.subSerializers.Add(typeToCache, new XmlSerializer <Object>(typeToCache, this.subSerializers, this.Settings)); } catch (Exception exception) { var ex = new XmlSerializationException( "The type is not serializable. See inner exception for more details.", exception); ex.Data.Add("Type", typeToCache); throw ex; } } } }