private void ImportSystemExceptionDataMembers() { /* * The data members imported for System.Exception are private fields. They must be treated specially. * The EssentialExceptionFields Dictionary keeps track of which private fields needs to be imported, and also the name that they should be serialized with. */ Type type = this.UnderlyingType; List <DataMember> tempMembers; tempMembers = new List <DataMember>(); Dictionary <string, DataMember> memberNamesTable = new Dictionary <string, DataMember>(); foreach (string fieldInfoName in EssentialExceptionFields.Keys) { FieldInfo member = type.GetField(fieldInfoName, BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance); if (CanSerializeMember(member)) { DataMember memberContract = new DataMember(member); memberContract.Name = DataContract.EncodeLocalName(member.Name); memberContract.IsRequired = false; memberContract.IsNullable = DataContract.IsTypeNullable(memberContract.MemberType); CheckAndAddMember(tempMembers, memberContract, memberNamesTable); } } Interlocked.MemoryBarrier(); _members = tempMembers; }
internal XmlQualifiedName GetExpandedStableName() { if (this.paramGenericInfos == null) { return(this.stableName); } return(new XmlQualifiedName(DataContract.EncodeLocalName(DataContract.ExpandGenericParameters(XmlConvert.DecodeName(this.stableName.Name), this)), this.stableName.Namespace)); }
private void ImportDataMembers() { /* * DataMembers are used for the deserialization of Exceptions. * DataMembers represent the fields and/or settable properties that the underlying Exception has. * The DataMembers are stored and eventually passed to a ClassDataContract created from the underlying Exception. * The ClassDataContract uses the list of DataMembers to set the fields/properties for the newly created Exception. * If a DataMember is a property it must be settable. */ Type type = this.UnderlyingType; if (type == Globals.TypeOfException) { ImportSystemExceptionDataMembers(); //System.Exception must be handled specially because it is the only exception that imports private fields. return; } List <DataMember> tempMembers; if (BaseContract != null) { tempMembers = new List <DataMember>(BaseContract.Members); //Don't set tempMembers = BaseContract.Members and then start adding, because this alters the base's reference. } else { tempMembers = new List <DataMember>(); } Dictionary <string, DataMember> memberNamesTable = new Dictionary <string, DataMember>(); MemberInfo[] memberInfos; memberInfos = type.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); for (int i = 0; i < memberInfos.Length; i++) { MemberInfo member = memberInfos[i]; //Public properties with set methods can be deserialized with the ClassDataContract PropertyInfo publicProperty = member as PropertyInfo; if (publicProperty != null && publicProperty.SetMethod != null) { DataMember memberContract = new DataMember(member); memberContract.Name = DataContract.EncodeLocalName(member.Name); memberContract.IsRequired = false; memberContract.IsNullable = DataContract.IsTypeNullable(memberContract.MemberType); if (HasNoConflictWithBaseMembers(memberContract)) { CheckAndAddMember(tempMembers, memberContract, memberNamesTable); } } } Interlocked.MemoryBarrier(); _members = tempMembers; }
internal void WriteSerializationInfo(XmlWriterDelegator xmlWriter, Type objType, SerializationInfo serInfo) { if (DataContract.GetClrTypeFullName(objType) != serInfo.FullTypeName) { if (base.DataContractResolver != null) { XmlDictionaryString str; XmlDictionaryString str2; if (this.ResolveType(serInfo.ObjectType, objType, out str, out str2)) { xmlWriter.WriteAttributeQualifiedName("z", DictionaryGlobals.ISerializableFactoryTypeLocalName, DictionaryGlobals.SerializationNamespace, str, str2); } } else { string str3; string str4; DataContract.GetDefaultStableName(serInfo.FullTypeName, out str3, out str4); xmlWriter.WriteAttributeQualifiedName("z", DictionaryGlobals.ISerializableFactoryTypeLocalName, DictionaryGlobals.SerializationNamespace, DataContract.GetClrTypeString(str3), DataContract.GetClrTypeString(str4)); } } this.WriteClrTypeInfo(xmlWriter, objType, serInfo); base.IncrementItemCount(serInfo.MemberCount); SerializationInfoEnumerator enumerator = serInfo.GetEnumerator(); while (enumerator.MoveNext()) { SerializationEntry current = enumerator.Current; XmlDictionaryString clrTypeString = DataContract.GetClrTypeString(DataContract.EncodeLocalName(current.Name)); xmlWriter.WriteStartElement(clrTypeString, DictionaryGlobals.EmptyString); object obj2 = current.Value; if (obj2 == null) { this.WriteNull(xmlWriter); } else { this.InternalSerializeReference(xmlWriter, obj2, false, false, -1, Globals.TypeOfObject.TypeHandle); } xmlWriter.WriteEndElement(); } }
internal void WriteSerializationInfo(XmlWriterDelegator xmlWriter, Type objType, SerializationInfo serInfo) { if (DataContract.GetClrTypeFullName(objType) != serInfo.FullTypeName) { if (DataContractResolver != null) { XmlDictionaryString typeName, typeNs; if (ResolveType(serInfo.ObjectType, objType, out typeName, out typeNs)) { xmlWriter.WriteAttributeQualifiedName(Globals.SerPrefix, DictionaryGlobals.ISerializableFactoryTypeLocalName, DictionaryGlobals.SerializationNamespace, typeName, typeNs); } } else { string typeName, typeNs; DataContract.GetDefaultStableName(serInfo.FullTypeName, out typeName, out typeNs); xmlWriter.WriteAttributeQualifiedName(Globals.SerPrefix, DictionaryGlobals.ISerializableFactoryTypeLocalName, DictionaryGlobals.SerializationNamespace, DataContract.GetClrTypeString(typeName), DataContract.GetClrTypeString(typeNs)); } } WriteClrTypeInfo(xmlWriter, objType, serInfo); IncrementItemCount(serInfo.MemberCount); foreach (SerializationEntry serEntry in serInfo) { XmlDictionaryString name = DataContract.GetClrTypeString(DataContract.EncodeLocalName(serEntry.Name)); xmlWriter.WriteStartElement(name, DictionaryGlobals.EmptyString); object obj = serEntry.Value; if (obj == null) { WriteNull(xmlWriter); } else { InternalSerializeReference(xmlWriter, obj, false /*isDeclaredType*/, false /*writeXsiType*/, -1, Globals.TypeOfObject.TypeHandle); } xmlWriter.WriteEndElement(); } }
internal XmlDataContractCriticalHelper(Type type) : base(type) { if (type.IsDefined(Globals.TypeOfDataContractAttribute, false)) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.IXmlSerializableCannotHaveDataContract, DataContract.GetClrTypeFullName(type)))); } if (type.IsDefined(Globals.TypeOfCollectionDataContractAttribute, false)) { throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.IXmlSerializableCannotHaveCollectionDataContract, DataContract.GetClrTypeFullName(type)))); } XmlSchemaType xsdType; bool hasRoot; XmlQualifiedName stableName; SchemaExporter.GetXmlTypeInfo(type, out stableName, out xsdType, out hasRoot); this.StableName = stableName; this.HasRoot = hasRoot; XmlDictionary dictionary = new XmlDictionary(); this.Name = dictionary.Add(StableName.Name); this.Namespace = dictionary.Add(StableName.Namespace); object[] xmlRootAttributes = (UnderlyingType == null) ? null : UnderlyingType.GetCustomAttributes(Globals.TypeOfXmlRootAttribute, false).ToArray(); if (xmlRootAttributes == null || xmlRootAttributes.Length == 0) { if (hasRoot) { _topLevelElementName = Name; _topLevelElementNamespace = (this.StableName.Namespace == Globals.SchemaNamespace) ? DictionaryGlobals.EmptyString : Namespace; _isTopLevelElementNullable = true; } } else { if (hasRoot) { XmlRootAttribute xmlRootAttribute = (XmlRootAttribute)xmlRootAttributes[0]; _isTopLevelElementNullable = xmlRootAttribute.IsNullable; string elementName = xmlRootAttribute.ElementName; _topLevelElementName = (elementName == null || elementName.Length == 0) ? Name : dictionary.Add(DataContract.EncodeLocalName(elementName)); string elementNs = xmlRootAttribute.Namespace; _topLevelElementNamespace = (elementNs == null || elementNs.Length == 0) ? DictionaryGlobals.EmptyString : dictionary.Add(elementNs); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.Format(SR.IsAnyCannotHaveXmlRoot, DataContract.GetClrTypeFullName(UnderlyingType)))); } } }
internal XmlDataContractCriticalHelper(Type type) : base(type) { XmlSchemaType type2; bool flag; XmlQualifiedName name; if (type.IsDefined(Globals.TypeOfDataContractAttribute, false)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("IXmlSerializableCannotHaveDataContract", new object[] { DataContract.GetClrTypeFullName(type) }))); } if (type.IsDefined(Globals.TypeOfCollectionDataContractAttribute, false)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("IXmlSerializableCannotHaveCollectionDataContract", new object[] { DataContract.GetClrTypeFullName(type) }))); } SchemaExporter.GetXmlTypeInfo(type, out name, out type2, out flag); base.StableName = name; this.XsdType = type2; this.HasRoot = flag; XmlDictionary dictionary = new XmlDictionary(); base.Name = dictionary.Add(base.StableName.Name); base.Namespace = dictionary.Add(base.StableName.Namespace); object[] objArray = (base.UnderlyingType == null) ? null : base.UnderlyingType.GetCustomAttributes(Globals.TypeOfXmlRootAttribute, false); if ((objArray == null) || (objArray.Length == 0)) { if (flag) { this.topLevelElementName = base.Name; this.topLevelElementNamespace = (base.StableName.Namespace == "http://www.w3.org/2001/XMLSchema") ? DictionaryGlobals.EmptyString : base.Namespace; this.isTopLevelElementNullable = true; } } else { if (!flag) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(System.Runtime.Serialization.SR.GetString("IsAnyCannotHaveXmlRoot", new object[] { DataContract.GetClrTypeFullName(base.UnderlyingType) }))); } XmlRootAttribute attribute = (XmlRootAttribute)objArray[0]; this.isTopLevelElementNullable = attribute.IsNullable; string elementName = attribute.ElementName; this.topLevelElementName = ((elementName == null) || (elementName.Length == 0)) ? base.Name : dictionary.Add(DataContract.EncodeLocalName(elementName)); string str2 = attribute.Namespace; this.topLevelElementNamespace = ((str2 == null) || (str2.Length == 0)) ? DictionaryGlobals.EmptyString : dictionary.Add(str2); } }