Exemplo n.º 1
0
        protected override void DoToXml(object parent, PropertyDescriptor metadata, object entity, XmlTextWriter writer, XmlSerializerContext context)
        {
            Type           entityType           = entity.GetType();
            TypeDescriptor entityTypeDescriptor = context.GetTypeDescriptor(entityType);

            //no es serializable...
            if (entityTypeDescriptor == null)
            {
                return;
            }

            ObjectPropertyDescriptor descriptor = metadata.GetPropertyDescriptor <ObjectPropertyDescriptor>(entityType, context);

            string elementName = metadata.GetElementNameForType(entityType, context, true);

            //escribo el nombre de la propiedad
            writer.WriteStartElement(elementName);

            if (!context.Settings.UniqueSerializationForInstance || !context.Stack.ContainsInstance(entity))
            {
                //agrego la lista a las entidades registradas
                long id = context.Stack.AddInstance(entity);

                //escribo las propiedades que son atributos
                this.WriteProperties(metadata, entity, entityTypeDescriptor, writer, context, true);

                //agrego el tipo de la entidad como ultimo atributo
                base.WriteTypeDefinition(descriptor, entityType, context, writer);

                //escribo el id del objeto
                if (context.Settings.UniqueSerializationForInstance)
                {
                    writer.WriteAttributeString(XmlSerializerSettings.ObjectIdAttributeName, id.ToString());
                }

                //escribo las propiedades que son elementos
                this.WriteProperties(metadata, entity, entityTypeDescriptor, writer, context, false);
            }
            else
            {
                //me fijo si ya existe en el context
                long id = context.Stack.GetInstanceReferenceId(entity);
                writer.WriteAttributeString(XmlSerializerSettings.ObjectReferenceAttributeName, id.ToString());
            }

            //escribo el nombre de la propiedad
            writer.WriteEndElement();
        }
Exemplo n.º 2
0
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            if (_customProperties == null)
            {
                PropertyDescriptor[] properties = new PropertyDescriptor[_originalProperties.Count];

                for (int i = 0; i < properties.Length; i++)
                {
                    properties[i] = new ObjectPropertyDescriptor(_originalProperties[i]);
                }

                _customProperties = new PropertyDescriptorCollection(properties);
            }

            return(_customProperties);
        }
Exemplo n.º 3
0
		PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
		{
			if (_customProperties == null)
			{
				PropertyDescriptor[] properties = new PropertyDescriptor[_originalProperties.Count];

				for (int i = 0; i < properties.Length; i++)
				{
					properties[i] = new ObjectPropertyDescriptor(_originalProperties[i]);
				}

				_customProperties = new PropertyDescriptorCollection(properties);
			}

			return _customProperties;
		}
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="metadata"></param>
        /// <param name="entity"></param>
        /// <param name="writer"></param>
        /// <param name="context"></param>
        protected override void DoToXml(object parent, PropertyDescriptor metadata, object entity, XmlTextWriter writer, XmlSerializerContext context)
        {
            ConfigurationSection section = (ConfigurationSection)entity;

            if (section == null)
            {
                return;
            }

            Type   entityType  = section.GetType();
            string elementName = metadata.GetElementNameForType(entityType, context, true);

            if (!context.Settings.UniqueSerializationForInstance || !context.Stack.ContainsInstance(entity))
            {
                //agrego la lista a las entidades registradas
                long   id       = context.Stack.AddInstance(entity);
                string valueStr = (string)SerializeMethod.Invoke(section, new object[] { null, elementName, ConfigurationSaveMode.Full });

                //escribo el nombre del elemento...
                writer.WriteStartElement(elementName);

                //recorro el contenido
                StringReader  stringReader = new StringReader(valueStr);
                XmlTextReader reader       = new XmlTextReader(stringReader);

                try
                {
                    reader.MoveToContent();
                    //recorro todos los attributos y los escribo en el destino
                    if (reader.MoveToFirstAttribute())
                    {
                        writer.WriteAttributeString(reader.LocalName, reader.Value);
                        while (reader.MoveToNextAttribute())
                        {
                            writer.WriteAttributeString(reader.LocalName, reader.Value);
                        }
                    }

                    //me fijo si hay que escribir el id
                    //escribo el id del objeto si corresponde
                    if (context.Settings.UniqueSerializationForInstance)
                    {
                        writer.WriteAttributeString(XmlSerializerSettings.ObjectIdAttributeName, id.ToString());
                    }

                    //agrego el tipo de la entidad como ultimo atributo
                    ObjectPropertyDescriptor descriptor = metadata.GetPropertyDescriptor <ObjectPropertyDescriptor>(entityType, context);
                    base.WriteTypeDefinition(descriptor, entityType, context, writer);

                    reader.MoveToContent();
                    string str = reader.ReadInnerXml();
                    if (!string.IsNullOrEmpty(str))
                    {
                        writer.WriteRaw(str);
                    }
                }
                finally
                {
                    stringReader.Close();
                    reader.Close();
                }

                //cierro el tag
                writer.WriteEndElement();
            }
            else
            {
                writer.WriteStartElement(elementName);

                //me fijo si ya existe en el context
                long id = context.Stack.GetInstanceReferenceId(entity);
                writer.WriteAttributeString(XmlSerializerSettings.ObjectReferenceAttributeName, id.ToString());

                writer.WriteEndElement();
            }
        }