Esempio n. 1
0
        static string GetWhatWasFound(XmlDictionaryReader reader)
        {
            if (reader.EOF)
            {
                return(SR_.GetString(SR_.XmlFoundEndOfFile));
            }
            switch (reader.NodeType)
            {
            case XmlNodeType.Element:
                return(SR_.GetString(SR_.XmlFoundElement, GetName(reader.Prefix, reader.LocalName), reader.NamespaceURI));

            case XmlNodeType.EndElement:
                return(SR_.GetString(SR_.XmlFoundEndElement, GetName(reader.Prefix, reader.LocalName), reader.NamespaceURI));

            case XmlNodeType.Text:
            case XmlNodeType.Whitespace:
            case XmlNodeType.SignificantWhitespace:
                return(SR_.GetString(SR_.XmlFoundText, reader.Value));

            case XmlNodeType.Comment:
                return(SR_.GetString(SR_.XmlFoundComment, reader.Value));

            case XmlNodeType.CDATA:
                return(SR_.GetString(SR_.XmlFoundCData, reader.Value));
            }
            return(SR_.GetString(SR_.XmlFoundNodeType, reader.NodeType));
        }
Esempio n. 2
0
        static public void ThrowXmlException(XmlDictionaryReader reader, XmlException exception)
        {
            string       s        = exception.Message;
            IXmlLineInfo lineInfo = reader as IXmlLineInfo;

            if (lineInfo != null && lineInfo.HasLineInfo())
            {
                s += " " + SR_.GetString(SR_.XmlLineInfo, lineInfo.LineNumber, lineInfo.LinePosition);
            }
            throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(s));
        }
Esempio n. 3
0
        static void ThrowXmlException(XmlDictionaryReader reader, string res, string arg1, string arg2, string arg3)
        {
            string       s        = SR_.GetString(res, arg1, arg2, arg3);
            IXmlLineInfo lineInfo = reader as IXmlLineInfo;

            if (lineInfo != null && lineInfo.HasLineInfo())
            {
                s += " " + SR_.GetString(SR_.XmlLineInfo, lineInfo.LineNumber, lineInfo.LinePosition);
            }

            if (TD.ReaderQuotaExceededIsEnabled())
            {
                TD.ReaderQuotaExceeded(s);
            }

            throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(s));
        }
Esempio n. 4
0
 internal DataContract GetMemberTypeDataContract(DataMember dataMember)
 {
     if (dataMember.MemberInfo != null)
     {
         Type dataMemberType = dataMember.MemberType;
         if (dataMember.IsGetOnlyCollection)
         {
             if (dataContractSurrogate != null)
             {
                 Type dcType = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, dataMemberType);
                 if (dcType != dataMemberType)
                 {
                     throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.SurrogatesWithGetOnlyCollectionsNotSupported,
                                                                                                                                                          DataContract.GetClrTypeFullName(dataMemberType), DataContract.GetClrTypeFullName(dataMember.MemberInfo.DeclaringType), dataMember.MemberInfo.Name)));
                 }
             }
             return(DataContract.GetGetOnlyCollectionDataContract(DataContract.GetId(dataMemberType.TypeHandle), dataMemberType.TypeHandle, dataMemberType, SerializationMode.SharedContract));
         }
         else
         {
             return(GetDataContract(dataMemberType));
         }
     }
     return(dataMember.MemberTypeContract);
 }
Esempio n. 5
0
        internal void InternalAdd(XmlQualifiedName name, DataContract dataContract)
        {
            DataContract dataContractInSet = null;

            if (Contracts.TryGetValue(name, out dataContractInSet))
            {
                if (!dataContractInSet.Equals(dataContract))
                {
                    if (dataContract.UnderlyingType == null || dataContractInSet.UnderlyingType == null)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.DupContractInDataContractSet, dataContract.StableName.Name, dataContract.StableName.Namespace)));
                    }
                    else
                    {
                        bool typeNamesEqual = (DataContract.GetClrTypeFullName(dataContract.UnderlyingType) == DataContract.GetClrTypeFullName(dataContractInSet.UnderlyingType));
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.DupTypeContractInDataContractSet, (typeNamesEqual ? dataContract.UnderlyingType.AssemblyQualifiedName : DataContract.GetClrTypeFullName(dataContract.UnderlyingType)), (typeNamesEqual ? dataContractInSet.UnderlyingType.AssemblyQualifiedName : DataContract.GetClrTypeFullName(dataContractInSet.UnderlyingType)), dataContract.StableName.Name, dataContract.StableName.Namespace)));
                    }
                }
            }
            else
            {
                Contracts.Add(name, dataContract);

                if (dataContract is ClassDataContract)
                {
                    AddClassDataContract((ClassDataContract)dataContract);
                }
                else if (dataContract is CollectionDataContract)
                {
                    AddCollectionDataContract((CollectionDataContract)dataContract);
                }
                else if (dataContract is XmlDataContract)
                {
                    AddXmlDataContract((XmlDataContract)dataContract);
                }
            }
        }
Esempio n. 6
0
 internal static void EnsureTypeNotGeneric(Type type)
 {
     if (type.ContainsGenericParameters)
     {
         throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.GenericTypeNotExportable, type)));
     }
 }
            LocalBuilder ReadValue(Type type, string name, string ns)
            {
                LocalBuilder value         = ilg.DeclareLocal(type, "valueRead");
                LocalBuilder nullableValue = null;
                int          nullables     = 0;

                while (type.IsGenericType && type.GetGenericTypeDefinition() == Globals.TypeOfNullable)
                {
                    nullables++;
                    type = type.GetGenericArguments()[0];
                }

                PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(type);

                if ((primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject) || nullables != 0 || type.IsValueType)
                {
                    LocalBuilder objectId = ilg.DeclareLocal(Globals.TypeOfString, "objectIdRead");
                    ilg.Call(contextArg, XmlFormatGeneratorStatics.ReadAttributesMethod, xmlReaderArg);
                    ilg.Call(contextArg, XmlFormatGeneratorStatics.ReadIfNullOrRefMethod, xmlReaderArg, type, DataContract.IsTypeSerializable(type));
                    ilg.Stloc(objectId);
                    // Deserialize null
                    ilg.If(objectId, Cmp.EqualTo, Globals.NullObjectId);
                    if (nullables != 0)
                    {
                        ilg.LoadAddress(value);
                        ilg.InitObj(value.LocalType);
                    }
                    else if (type.IsValueType)
                    {
                        ThrowValidationException(SR.GetString(SR.ValueTypeCannotBeNull, DataContract.GetClrTypeFullName(type)));
                    }
                    else
                    {
                        ilg.Load(null);
                        ilg.Stloc(value);
                    }

                    // Deserialize value

                    // Compare against Globals.NewObjectId, which is set to string.Empty
                    ilg.ElseIfIsEmptyString(objectId);
                    ilg.Call(contextArg, XmlFormatGeneratorStatics.GetObjectIdMethod);
                    ilg.Stloc(objectId);
                    if (type.IsValueType)
                    {
                        ilg.IfNotIsEmptyString(objectId);
                        ThrowValidationException(SR.GetString(SR.ValueTypeCannotHaveId, DataContract.GetClrTypeFullName(type)));
                        ilg.EndIf();
                    }
                    if (nullables != 0)
                    {
                        nullableValue = value;
                        value         = ilg.DeclareLocal(type, "innerValueRead");
                    }

                    if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject)
                    {
                        ilg.Call(xmlReaderArg, primitiveContract.XmlFormatReaderMethod);
                        ilg.Stloc(value);
                        if (!type.IsValueType)
                        {
                            ilg.Call(contextArg, XmlFormatGeneratorStatics.AddNewObjectMethod, value);
                        }
                    }
                    else
                    {
                        InternalDeserialize(value, type, name, ns);
                    }
                    // Deserialize ref
                    ilg.Else();
                    if (type.IsValueType)
                    {
                        ThrowValidationException(SR.GetString(SR.ValueTypeCannotHaveRef, DataContract.GetClrTypeFullName(type)));
                    }
                    else
                    {
                        ilg.Call(contextArg, XmlFormatGeneratorStatics.GetExistingObjectMethod, objectId, type, name, ns);
                        ilg.ConvertValue(Globals.TypeOfObject, type);
                        ilg.Stloc(value);
                    }
                    ilg.EndIf();

                    if (nullableValue != null)
                    {
                        ilg.If(objectId, Cmp.NotEqualTo, Globals.NullObjectId);
                        WrapNullableObject(value, nullableValue, nullables);
                        ilg.EndIf();
                        value = nullableValue;
                    }
                }
                else
                {
                    InternalDeserialize(value, type, name, ns);
                }

                return(value);
            }
Esempio n. 8
0
        bool TryGetReferencedType(XmlQualifiedName stableName, DataContract dataContract, bool useReferencedCollectionTypes, out Type type)
        {
            object value;
            Dictionary <XmlQualifiedName, object> referencedTypes = useReferencedCollectionTypes ? GetReferencedCollectionTypes() : GetReferencedTypes();

            if (referencedTypes.TryGetValue(stableName, out value))
            {
                type = value as Type;
                if (type != null)
                {
                    return(true);
                }
                else
                {
                    // Throw ambiguous type match exception
                    List <Type>   types               = (List <Type>)value;
                    StringBuilder errorMessage        = new StringBuilder();
                    bool          containsGenericType = false;
                    for (int i = 0; i < types.Count; i++)
                    {
                        Type conflictingType = types[i];
                        if (!containsGenericType)
                        {
                            containsGenericType = conflictingType.IsGenericTypeDefinition;
                        }
                        errorMessage.AppendFormat("{0}\"{1}\" ", Environment.NewLine, conflictingType.AssemblyQualifiedName);
                        if (dataContract != null)
                        {
                            DataContract other = this.GetDataContract(conflictingType);
                            errorMessage.Append(SR.GetString(((other != null && other.Equals(dataContract)) ? SR.ReferencedTypeMatchingMessage : SR.ReferencedTypeNotMatchingMessage)));
                        }
                    }
                    if (containsGenericType)
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
                                                                                                                                                 (useReferencedCollectionTypes ? SR.AmbiguousReferencedCollectionTypes1 : SR.AmbiguousReferencedTypes1),
                                                                                                                                                 errorMessage.ToString())));
                    }
                    else
                    {
                        throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(
                                                                                                                                                 (useReferencedCollectionTypes ? SR.AmbiguousReferencedCollectionTypes3 : SR.AmbiguousReferencedTypes3),
                                                                                                                                                 XmlConvert.DecodeName(stableName.Name),
                                                                                                                                                 stableName.Namespace,
                                                                                                                                                 errorMessage.ToString())));
                    }
                }
            }
            type = null;
            return(false);
        }
Esempio n. 9
0
        public void WriteISerializable(XmlWriterDelegator xmlWriter, ISerializable obj)
        {
            Type objType = obj.GetType();
            SerializationInfo serInfo = new SerializationInfo(objType, XmlObjectSerializer.FormatterConverter, !this.UnsafeTypeForwardingEnabled);

            GetObjectData(obj, serInfo, GetStreamingContext());

            if (!this.UnsafeTypeForwardingEnabled && serInfo.AssemblyName == Globals.MscorlibAssemblyName)
            {
                // Throw if a malicious type tries to set its assembly name to "0" to get deserialized in mscorlib
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.ISerializableAssemblyNameSetToZero, DataContract.GetClrTypeFullName(obj.GetType()))));
            }

            WriteSerializationInfo(xmlWriter, objType, serInfo);
        }
Esempio n. 10
0
        static void WriteIXmlSerializable(XmlWriterDelegator xmlWriter, object obj, XmlSerializableWriter xmlSerializableWriter)
        {
            xmlSerializableWriter.BeginWrite(xmlWriter.Writer, obj);
            IXmlSerializable xmlSerializable = obj as IXmlSerializable;

            if (xmlSerializable != null)
            {
                xmlSerializable.WriteXml(xmlSerializableWriter);
            }
            else
            {
                XmlElement xmlElement = obj as XmlElement;
                if (xmlElement != null)
                {
                    xmlElement.WriteTo(xmlSerializableWriter);
                }
                else
                {
                    XmlNode[] xmlNodes = obj as XmlNode[];
                    if (xmlNodes != null)
                    {
                        foreach (XmlNode xmlNode in xmlNodes)
                        {
                            xmlNode.WriteTo(xmlSerializableWriter);
                        }
                    }
                    else
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.UnknownXmlType, DataContract.GetClrTypeFullName(obj.GetType()))));
                    }
                }
            }
            xmlSerializableWriter.EndWrite();
        }
Esempio n. 11
0
 public static void ThrowRequiredMemberMustBeEmitted(string memberName, Type type)
 {
     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.RequiredMemberMustBeEmitted, memberName, type.FullName)));
 }
Esempio n. 12
0
 internal virtual bool OnHandleReference(XmlWriterDelegator xmlWriter, object obj, bool canContainCyclicReference)
 {
     if (xmlWriter.depth < depthToCheckCyclicReference)
     {
         return(false);
     }
     if (canContainCyclicReference)
     {
         if (byValObjectsInScope.Count == 0 && DiagnosticUtility.ShouldTraceWarning)
         {
             TraceUtility.Trace(TraceEventType.Warning, TraceCode.ObjectWithLargeDepth, SR.GetString(SR.TraceCodeObjectWithLargeDepth));
         }
         if (byValObjectsInScope.Contains(obj))
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.CannotSerializeObjectWithCycles, DataContract.GetClrTypeFullName(obj.GetType()))));
         }
         byValObjectsInScope.Push(obj);
     }
     return(false);
 }
Esempio n. 13
0
        protected void SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, bool verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
        {
            bool knownTypesAddedInCurrentScope = false;

            if (dataContract.KnownDataContracts != null)
            {
                scopedKnownTypes.Push(dataContract.KnownDataContracts);
                knownTypesAddedInCurrentScope = true;
            }

            if (verifyKnownType)
            {
                if (!IsKnownType(dataContract, declaredType))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.DcTypeNotFoundOnSerialize, DataContract.GetClrTypeFullName(dataContract.UnderlyingType), dataContract.StableName.Name, dataContract.StableName.Namespace)));
                }
            }
            WriteDataContractValue(dataContract, xmlWriter, obj, declaredTypeHandle);

            if (knownTypesAddedInCurrentScope)
            {
                scopedKnownTypes.Pop();
            }
        }
Esempio n. 14
0
        internal void Add(string id, object obj)
        {
            if (objectDictionary == null)
            {
                objectDictionary = new Dictionary <string, object>();
            }

            object existingObject;

            if (objectDictionary.TryGetValue(id, out existingObject))
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.MultipleIdDefinition, id)));
            }
            objectDictionary.Add(id, obj);
        }
Esempio n. 15
0
 internal Dictionary <string, object> GetMemberNames()
 {
     if (ReferencedTypeExists)
     {
         throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CannotSetMembersForReferencedType, TypeReference.BaseType)));
     }
     else
     {
         if (memberNames == null)
         {
             memberNames = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);
         }
         return(memberNames);
     }
 }
Esempio n. 16
0
        Dictionary <XmlQualifiedName, object> GetReferencedTypes()
        {
            if (referencedTypesDictionary == null)
            {
                referencedTypesDictionary = new Dictionary <XmlQualifiedName, object>();
                //Always include Nullable as referenced type
                //Do not allow surrogating Nullable<T>
                referencedTypesDictionary.Add(DataContract.GetStableName(Globals.TypeOfNullable), Globals.TypeOfNullable);
                if (this.referencedTypes != null)
                {
                    foreach (Type type in this.referencedTypes)
                    {
                        if (type == null)
                        {
                            throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ReferencedTypesCannotContainNull)));
                        }

                        AddReferencedType(referencedTypesDictionary, type);
                    }
                }
            }
            return(referencedTypesDictionary);
        }
Esempio n. 17
0
 Dictionary <XmlQualifiedName, object> GetReferencedCollectionTypes()
 {
     if (referencedCollectionTypesDictionary == null)
     {
         referencedCollectionTypesDictionary = new Dictionary <XmlQualifiedName, object>();
         if (this.referencedCollectionTypes != null)
         {
             foreach (Type type in this.referencedCollectionTypes)
             {
                 if (type == null)
                 {
                     throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ReferencedCollectionTypesCannotContainNull)));
                 }
                 AddReferencedType(referencedCollectionTypesDictionary, type);
             }
         }
         XmlQualifiedName genericDictionaryName = DataContract.GetStableName(Globals.TypeOfDictionaryGeneric);
         if (!referencedCollectionTypesDictionary.ContainsKey(genericDictionaryName) && GetReferencedTypes().ContainsKey(genericDictionaryName))
         {
             AddReferencedType(referencedCollectionTypesDictionary, Globals.TypeOfDictionaryGeneric);
         }
     }
     return(referencedCollectionTypesDictionary);
 }
Esempio n. 18
0
 bool ResolveType(Type objectType, Type declaredType, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
 {
     if (!DataContractResolver.TryResolveType(objectType, declaredType, KnownTypeResolver, out typeName, out typeNamespace))
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.ResolveTypeReturnedFalse, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType))));
     }
     if (typeName == null)
     {
         if (typeNamespace == null)
         {
             return(false);
         }
         else
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.ResolveTypeReturnedNull, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType))));
         }
     }
     if (typeNamespace == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.ResolveTypeReturnedNull, DataContract.GetClrTypeFullName(DataContractResolver.GetType()), DataContract.GetClrTypeFullName(objectType))));
     }
     return(true);
 }
Esempio n. 19
0
 static public XmlException CreateConversionException(string value, string type, Exception exception)
 {
     return(new XmlException(SR_.GetString(SR_.XmlInvalidConversion, value, type), exception));
 }
Esempio n. 20
0
 public void IncrementItemCount(int count)
 {
     if (count > maxItemsInObjectGraph - itemCount)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.GetString(SR.ExceededMaxItemsQuota, maxItemsInObjectGraph)));
     }
     itemCount += count;
 }
Esempio n. 21
0
 static public XmlException CreateEncodingException(string value, Exception exception)
 {
     return(new XmlException(SR_.GetString(SR_.XmlInvalidUTF8Bytes, value), exception));
 }
Esempio n. 22
0
 internal virtual void CheckIfTypeSerializable(Type memberType, bool isMemberTypeSerializable)
 {
     if (!isMemberTypeSerializable)
     {
         throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.TypeNotSerializable, memberType)));
     }
 }
Esempio n. 23
0
        internal static DataContractDictionary GetDataContractsForKnownTypes(IList <Type> knownTypeList)
        {
            if (knownTypeList == null)
            {
                return(null);
            }
            DataContractDictionary  dataContracts = new DataContractDictionary();
            Dictionary <Type, Type> typesChecked  = new Dictionary <Type, Type>();

            for (int i = 0; i < knownTypeList.Count; i++)
            {
                Type knownType = knownTypeList[i];
                if (knownType == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.NullKnownType, "knownTypes")));
                }

                DataContract.CheckAndAdd(knownType, typesChecked, ref dataContracts);
            }
            return(dataContracts);
        }
Esempio n. 24
0
        ConstructorInfo GetConstructor()
        {
            Type type = contract.UnderlyingType;

            if (type.IsValueType)
            {
                return(null);
            }

            ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Globals.EmptyTypeArray, null);

            if (ctor == null)
            {
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataContractException(SR.GetString(SR.IXmlSerializableMustHaveDefaultConstructor, DataContract.GetClrTypeFullName(type))));
            }

            return(ctor);
        }