示例#1
0
 /// <summary>Writes a single object to the output XML stream, using the specified type hint.</summary>
 /// <param name="value">The value to write.</param>
 /// <param name="format">The format of the XML.</param>
 /// <param name="typeSerializer">The type serializer.</param>
 public void WriteObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     if (format == null)
     {
         throw new ArgumentNullException("format");
     }
     if (typeSerializer == null)
     {
         throw new ArgumentNullException("typeSerializer");
     }
     if (!format.FlattenContent)
     {
         if (string.IsNullOrEmpty(format.ElementName))
         {
             throw new ArgumentException(Resources.NullElementName);
         }
         this.Xml.WriteStartElement(format.ElementName);
     }
     if (value == null)
     {
         if (format.FlattenContent)
         {
             throw new InvalidOperationException(Resources.CantWriteNullInFlattenContentMode);
         }
         this.Xml.WriteAttributeString("Null", "true");
     }
     else
     {
         Type type = value.GetType();
         if (type.IsSubclassOf(typeof(Type)))
         {
             type = typeof(Type);
         }
         if (type != typeSerializer.BoxedTargetType)
         {
             if (format.FlattenContent)
             {
                 throw new InvalidOperationException(Resources.CantWriteDynamicTypesInFlattenContentMode);
             }
             typeSerializer = this.Serializer.GetTypeSerializer(type);
             this.Xml.WriteStartAttribute("Type");
             this.WriteTypeName(typeSerializer.TargetType);
             this.Xml.WriteEndAttribute();
         }
         if (this.recurseDetector.ContainsKey(value))
         {
             throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.FoundCyclicReference, new object[]
             {
                 value
             }));
         }
         this.recurseDetector.Add(value, true);
         typeSerializer.Serialize(this, value, format);
         this.recurseDetector.Remove(value);
     }
     if (!format.FlattenContent)
     {
         this.Xml.WriteEndElement();
     }
 }
示例#2
0
 /// <summary>Writes a single object to the output XML stream as an instance of the specified type.</summary>
 /// <param name="value">The value to write.</param>
 /// <param name="format">The format of the XML.</param>
 /// <param name="typeSerializer">The type serializer.</param>
 public void WriteRawObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     if (value == null)
     {
         throw new ArgumentException("value");
     }
     if (format == null)
     {
         throw new ArgumentNullException("format");
     }
     if (typeSerializer == null)
     {
         throw new ArgumentNullException("typeSerializer");
     }
     if (!format.FlattenContent)
     {
         if (string.IsNullOrEmpty(format.ElementName))
         {
             throw new ArgumentException(Resources.NullElementName);
         }
         this.Xml.WriteStartElement(format.ElementName);
     }
     typeSerializer.Serialize(this, value, format);
     if (!format.FlattenContent)
     {
         this.Xml.WriteEndElement();
     }
 }
 public void ScanElements(ContentTypeSerializer.ChildCallback callback, object collection)
 {
     this.ValidateCollectionType(collection);
     IEnumerable enumerable = (IEnumerable)collection;
     foreach (object current in enumerable)
     {
         if (current != null)
         {
             callback(this.contentSerializer, current);
         }
     }
 }
示例#4
0
        private T ReadObjectInternal <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, object existingInstance)
        {
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            if (typeSerializer == null)
            {
                throw new ArgumentNullException("typeSerializer");
            }
            if (!format.FlattenContent)
            {
                if (!this.MoveToElement(format.ElementName))
                {
                    throw new Exception("Element not found.");
                }

                string attribute = this.Xml.GetAttribute("Null");
                if (attribute != null && XmlConvert.ToBoolean(attribute))
                {
                    if (!format.AllowNull)
                    {
                        throw new Exception("null element not allowed");
                    }
                    this.Xml.Skip();
                    return(default(T));
                }
                else
                {
                    if (this.Xml.MoveToAttribute("Type"))
                    {
                        Type type = this.ReadTypeName();
                        if (!typeSerializer.IsTargetType(type))
                        {
                            throw new Exception("Wrong xml type.");
                        }

                        typeSerializer = this.Serializer.GetTypeSerializer(type);
                        this.Xml.MoveToElement();
                    }
                    else
                    {
                        if (typeSerializer.TargetType == typeof(object))
                        {
                            throw new Exception("UnknownDeserializationType");
                        }
                    }
                }
            }
            return(this.ReadRawObjectInternal <T>(format, typeSerializer, existingInstance));
        }
示例#5
0
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     this.keySerializer           = serializer.GetTypeSerializer(typeof(Key));
     this.valueSerializer         = serializer.GetTypeSerializer(typeof(Value));
     this.keyFormat               = new ContentSerializerAttribute();
     this.valueFormat             = new ContentSerializerAttribute();
     this.keyFormat.ElementName   = "Key";
     this.valueFormat.ElementName = "Value";
     this.keyFormat.AllowNull     = false;
     if (typeof(Value).IsValueType)
     {
         this.valueFormat.AllowNull = false;
     }
 }
示例#6
0
        /// <summary>Deserializes an object from intermediate XML format.</summary>
        /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param>
        /// <param name="format">Specifies the intermediate source XML format.</param>
        /// <param name="existingInstance">The object containing the received data, or null if the deserializer should construct a new instance.</param>
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            T existingInstance2;

            if (existingInstance == null)
            {
                existingInstance2 = default(T);
            }
            else
            {
                existingInstance2 = ContentTypeSerializer <T> .CastType(existingInstance);
            }
            return(this.Deserialize(input, format, existingInstance2));
        }
示例#7
0
 public void AddXmlTypeName(ContentTypeSerializer serializer)
 {
     Type targetType = serializer.TargetType;
     string xmlTypeName = serializer.XmlTypeName;
     if (this.xmlNameToType.ContainsKey(xmlTypeName))
     {
         throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicateXmlTypeName, new object[]
         {
             serializer.GetType().AssemblyQualifiedName,
             this.xmlNameToType[xmlTypeName].AssemblyQualifiedName,
             xmlTypeName
         }));
     }
     this.typeToXmlName.Add(targetType, xmlTypeName);
     this.xmlNameToType.Add(xmlTypeName, targetType);
 }
示例#8
0
        public void AddXmlTypeName(ContentTypeSerializer serializer)
        {
            Type   targetType  = serializer.TargetType;
            string xmlTypeName = serializer.XmlTypeName;

            if (this.xmlNameToType.ContainsKey(xmlTypeName))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicateXmlTypeName, new object[]
                {
                    serializer.GetType().AssemblyQualifiedName,
                    this.xmlNameToType[xmlTypeName].AssemblyQualifiedName,
                    xmlTypeName
                }));
            }
            this.typeToXmlName.Add(targetType, xmlTypeName);
            this.xmlNameToType.Add(xmlTypeName, targetType);
        }
 private void AddTypeSerializer(ContentTypeSerializer serializer)
 {
     if (this.serializerInstances.ContainsKey(serializer.TargetType))
     {
         throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicateTypeHandler, new object[]
         {
             typeof(ContentTypeSerializer).Name,
             serializer.GetType().AssemblyQualifiedName,
             this.serializerInstances[serializer.TargetType].GetType().AssemblyQualifiedName,
             serializer.TargetType
         }));
     }
     if (serializer.XmlTypeName != null)
     {
         this.typeNameHelper.AddXmlTypeName(serializer);
     }
     this.serializerInstances.Add(serializer.TargetType, serializer);
 }
        protected internal override void Initialize(IntermediateSerializer serializer)
        {
            if (base.TargetType.BaseType != null)
            {
                this.baseSerializer = serializer.GetTypeSerializer(base.TargetType.BaseType);
            }
            MemberHelperBase <IntermediateSerializer> .CreateMemberHelpers <ReflectiveSerializerMemberHelper>(serializer, base.TargetType, this.memberHelpers);

            if (CollectionUtils.IsCollection(base.TargetType, false))
            {
                this.collectionHelper = serializer.GetCollectionHelper(base.TargetType);
            }
            object[] customAttributes = base.TargetType.GetCustomAttributes(typeof(ContentSerializerCollectionItemNameAttribute), true);
            if (customAttributes.Length == 1)
            {
                this.collectionItemName = ((ContentSerializerCollectionItemNameAttribute)customAttributes[0]).CollectionItemName;
            }
        }
        private void InitializeCommon(IntermediateSerializer serializer, MemberInfo memberInfo, Type memberType)
        {
            this.typeSerializer = serializer.GetTypeSerializer(memberType);
            Attribute customAttribute = Attribute.GetCustomAttribute(memberInfo, typeof(ContentSerializerAttribute));

            if (customAttribute == null)
            {
                this.formatAttribute             = new ContentSerializerAttribute();
                this.formatAttribute.ElementName = memberInfo.Name;
                return;
            }
            this.formatAttribute = (ContentSerializerAttribute)customAttribute;
            if (this.formatAttribute.ElementName == null)
            {
                this.formatAttribute             = this.formatAttribute.Clone();
                this.formatAttribute.ElementName = memberInfo.Name;
            }
        }
示例#12
0
 internal CollectionHelper(IntermediateSerializer serializer, Type type)
 {
     this.targetType = type;
     Type type2 = CollectionUtils.CollectionElementType(type, false);
     if (type2 == null)
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.NotACollectionType, new object[]
         {
             type
         }));
     }
     this.contentSerializer = serializer.GetTypeSerializer(type2);
     Type type3 = typeof(ICollection<>).MakeGenericType(new Type[]
     {
         type2
     });
     this.countPropertyGetter = ReflectionEmitUtils.GenerateGetter(type3.GetProperty("Count"));
     this.addToCollection = ReflectionEmitUtils.GenerateAddToCollection(type3, type2);
 }
示例#13
0
        internal CollectionHelper(IntermediateSerializer serializer, Type type)
        {
            this.targetType = type;
            Type type2 = CollectionUtils.CollectionElementType(type, false);

            if (type2 == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.NotACollectionType, new object[]
                {
                    type
                }));
            }
            this.contentSerializer = serializer.GetTypeSerializer(type2);
            Type type3 = typeof(ICollection <>).MakeGenericType(new Type[]
            {
                type2
            });

            this.countPropertyGetter = ReflectionEmitUtils.GenerateGetter(type3.GetProperty("Count"));
            this.addToCollection     = ReflectionEmitUtils.GenerateAddToCollection(type3, type2);
        }
示例#14
0
        private void ScanObject(ContentTypeSerializer typeSerializer, object value)
        {
            if (value == null)
            {
                return;
            }
            Type type = value.GetType();

            if (!type.IsValueType)
            {
                if (this.seenObjects.ContainsKey(value))
                {
                    return;
                }
                this.seenObjects.Add(value, true);
            }
            if (typeSerializer == null || type != typeSerializer.BoxedTargetType)
            {
                this.RecordType(type);
                typeSerializer = this.writer.Serializer.GetTypeSerializer(type);
            }
            typeSerializer.ScanChildren(this.writer.Serializer, new ContentTypeSerializer.ChildCallback(this.ScanObject), value);
        }
示例#15
0
 internal void WriteSharedResources()
 {
     if (this.sharedResources.Count > 0)
     {
         this.Xml.WriteStartElement("Resources");
         ContentSerializerAttribute contentSerializerAttribute = new ContentSerializerAttribute();
         contentSerializerAttribute.ElementName    = "Resource";
         contentSerializerAttribute.FlattenContent = true;
         while (this.sharedResources.Count > 0)
         {
             object obj  = this.sharedResources.Dequeue();
             Type   type = obj.GetType();
             ContentTypeSerializer typeSerializer = this.Serializer.GetTypeSerializer(type);
             this.Xml.WriteStartElement("Resource");
             this.Xml.WriteAttributeString("ID", this.sharedResourceNames[obj]);
             this.Xml.WriteStartAttribute("Type");
             this.WriteTypeName(type);
             this.Xml.WriteEndAttribute();
             this.WriteRawObject <object>(obj, contentSerializerAttribute, typeSerializer);
             this.Xml.WriteEndElement();
         }
         this.Xml.WriteEndElement();
     }
 }
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     this.underlyingTypeSerializer        = serializer.GetTypeSerializer(typeof(T));
     this.underlyingFormat                = new ContentSerializerAttribute();
     this.underlyingFormat.FlattenContent = true;
 }
 /// <summary>Examines the children of the specified object, passing each to a callback delegate.</summary>
 /// <param name="serializer">The content serializer.</param>
 /// <param name="callback">The method to be called for each examined child.</param>
 /// <param name="value">The object whose children are being scanned.</param>
 protected internal virtual void ScanChildren(IntermediateSerializer serializer, ContentTypeSerializer.ChildCallback callback, object value)
 {
 }
示例#18
0
 /// <summary>Reads a single object from the input XML stream using the specified type hint, optionally specifying an existing instance to receive the data.</summary>
 /// <param name="format">The format of the XML.</param>
 /// <param name="typeSerializer">The type serializer.</param>
 /// <param name="existingInstance">The object receiving the data, or null if a new instance should be created.</param>
 public T ReadObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
 {
     return(this.ReadObjectInternal <T>(format, typeSerializer, existingInstance));
 }
示例#19
0
 /// <summary>Reads a single object from the input XML stream, using the specified type hint.</summary>
 /// <param name="format">The format of the XML.</param>
 /// <param name="typeSerializer">The type serializer.</param>
 public T ReadObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     return(this.ReadObjectInternal <T>(format, typeSerializer, null));
 }
示例#20
0
        private T ReadRawObjectInternal <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, object existingInstance)
        {
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            if (typeSerializer == null)
            {
                throw new ArgumentNullException("typeSerializer");
            }
            object obj;

            if (format.FlattenContent)
            {
                this.Xml.MoveToContent();
                obj = typeSerializer.Deserialize(this, format, existingInstance);
            }
            else
            {
                if (!this.MoveToElement(format.ElementName))
                {
                    throw this.CreateInvalidContentException(Resources.ElementNotFound, new object[]
                    {
                        format.ElementName
                    });
                }
                XmlReader xmlReader = this.xmlReader;
                if (this.Xml.IsEmptyElement)
                {
                    this.xmlReader = FakeEmptyElementReader.Instance;
                }
                xmlReader.ReadStartElement();
                obj = typeSerializer.Deserialize(this, format, existingInstance);
                if (this.xmlReader == xmlReader)
                {
                    this.xmlReader.ReadEndElement();
                }
                else
                {
                    this.xmlReader = xmlReader;
                }
            }
            if (obj == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DeserializerReturnedNull, new object[]
                {
                    typeSerializer.GetType(),
                    typeSerializer.TargetType
                }));
            }
            if (existingInstance != null && obj != existingInstance)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DeserializerConstructedNewInstance, new object[]
                {
                    typeSerializer.GetType(),
                    typeSerializer.TargetType
                }));
            }
            if (!(obj is T))
            {
                throw this.CreateInvalidContentException(Resources.WrongXmlType, new object[]
                {
                    typeof(T),
                    obj.GetType()
                });
            }
            return((T)((object)obj));
        }
示例#21
0
 /// <summary>Examines the children of the specified object, passing each to a callback delegate.</summary>
 /// <param name="serializer">The content serializer.</param>
 /// <param name="callback">The method to be called for each examined child.</param>
 /// <param name="value">The object whose children are being scanned.</param>
 protected internal override void ScanChildren(IntermediateSerializer serializer, ContentTypeSerializer.ChildCallback callback, object value)
 {
     this.ScanChildren(serializer, callback, ContentTypeSerializer <T> .CastType(value));
 }
示例#22
0
 /// <summary>Queries whether a strongly-typed object contains data to be serialized.</summary>
 /// <param name="value">The object to query.</param>
 public override bool ObjectIsEmpty(object value)
 {
     return(this.ObjectIsEmpty(ContentTypeSerializer <T> .CastType(value)));
 }
示例#23
0
 /// <summary>Serializes an object to intermediate XML format.</summary>
 /// <param name="output">Specifies the intermediate XML location, and provides various serialization helpers.</param>
 /// <param name="value">The object to be serialized.</param>
 /// <param name="format">Specifies the content format for this object.</param>
 protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format)
 {
     this.Serialize(output, ContentTypeSerializer <T> .CastType(value), format);
 }