private void SerializeObjectSimple(SerializationInfo info, ResourceSerializationSettings settings)
 {
     foreach (AttributeValue a in this.Attributes)
     {
         a.SerializeValues(info, settings, a.AttributeName);
     }
 }
        /// <summary>
        /// Gets the values of the attribute converted to types suitable for serialization
        /// </summary>
        /// <param name="settings">The settings used to control serialization</param>
        /// <returns></returns>
        internal IList <object> GetSerializationValues(ResourceSerializationSettings settings)
        {
            List <object> serializationValues = new List <object>();

            if (this.Attribute.IsMultivalued)
            {
                foreach (object v in this.values)
                {
                    if (settings.ValueFormat == AttributeValueSerializationHandling.ConvertToString)
                    {
                        serializationValues.Add(TypeConverter.ToString(v));
                    }
                    else
                    {
                        serializationValues.Add(TypeConverter.ToSerializableValue(v));
                    }
                }
            }
            else
            {
                if (settings.ValueFormat == AttributeValueSerializationHandling.ConvertToString)
                {
                    serializationValues.Add(TypeConverter.ToString(this.value));
                }
                else
                {
                    serializationValues.Add(TypeConverter.ToSerializableValue(this.value));
                }
            }

            return(serializationValues);
        }
        /// <summary>
        /// Gets the object data for serialization
        /// </summary>
        /// <param name="info">The serialization data</param>
        /// <param name="context">The serialization context</param>
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            ResourceSerializationSettings settings = context.Context as ResourceSerializationSettings? ?? new ResourceSerializationSettings();

            if (settings.ResourceFormat == ResourceSerializationHandling.FixedStructure || settings.IncludePermissionHints)
            {
                this.SerializeObjectComplex(info, settings);
            }
            else
            {
                this.SerializeObjectSimple(info, settings);
            }
        }
        /// <summary>
        /// Gets a list of attributes and values for the resource in a primative format suitable for serialization
        /// </summary>
        /// <param name="settings">The settings used to serialize the resource</param>
        /// <returns>A list of attribute and value pairs</returns>
        internal Dictionary <string, IList <object> > GetSerializationValues(ResourceSerializationSettings settings)
        {
            Dictionary <string, IList <object> > values = new Dictionary <string, IList <object> >();

            foreach (AttributeValue kvp in this.attributes)
            {
                if (!kvp.IsNull)
                {
                    values.Add(kvp.AttributeName, kvp.GetSerializationValues(settings));
                }
            }

            return(values);
        }
        internal void SerializeValues(SerializationInfo info, ResourceSerializationSettings settings, string elementName)
        {
            if (this.IsNull)
            {
                if (settings.IncludeNullValues)
                {
                    if (settings.ArrayHandling == ArraySerializationHandling.AllAttributes ||
                        settings.ResourceFormat == ResourceSerializationHandling.FixedStructure ||
                        (this.Attribute.IsMultivalued && settings.ArrayHandling != ArraySerializationHandling.WhenRequired))
                    {
                        info.AddValue(elementName, new object[0]);
                    }
                    else
                    {
                        info.AddValue(elementName, null);
                    }
                }

                return;
            }

            IList <object> serializedValues = this.GetSerializationValues(settings);

            if (this.Attribute.IsMultivalued)
            {
                if (serializedValues.Count == 1 && settings.ArrayHandling == ArraySerializationHandling.WhenRequired && settings.ResourceFormat != ResourceSerializationHandling.FixedStructure)
                {
                    info.AddValue(elementName, serializedValues.First());
                }
                else
                {
                    info.AddValue(elementName, serializedValues);
                }
            }
            else
            {
                if (settings.ArrayHandling == ArraySerializationHandling.AllAttributes ||
                    settings.ResourceFormat == ResourceSerializationHandling.FixedStructure)
                {
                    info.AddValue(elementName, serializedValues);
                }
                else
                {
                    info.AddValue(elementName, serializedValues.First());
                }
            }
        }
        private void SerializeObjectComplex(SerializationInfo info, ResourceSerializationSettings settings)
        {
            if (settings.ResourceFormat == ResourceSerializationHandling.FixedStructure)
            {
                info.AddValue("Resource", this.Attributes.Where(t => !t.IsNull || settings.IncludeNullValues));
            }
            else
            {
                foreach (AttributeValue a in this.Attributes)
                {
                    if (a.IsNull && !settings.IncludeNullValues)
                    {
                        continue;
                    }

                    info.AddValue(a.AttributeName, a);
                }
            }
        }
        internal void Serialize(SerializationInfo info, ResourceSerializationSettings settings)
        {
            if (settings.ResourceFormat == ResourceSerializationHandling.FixedStructure)
            {
                info.AddValue("Name", this.AttributeName);
            }

            if (this.hasPermissionHint && settings.IncludePermissionHints)
            {
                List <string> permissions = this.PermissionHint.ToList();

                if (permissions.Count > 0)
                {
                    info.AddValue("PermissionHint", permissions);
                }
                else
                {
                    info.AddValue("PermissionHint", null);
                }
            }

            this.SerializeValues(info, settings, "Values");
        }
        /// <summary>Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with the data needed to serialize the target object.</summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> to populate with data. </param>
        /// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext" />) for this serialization. </param>
        /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            ResourceSerializationSettings settings = context.Context as ResourceSerializationSettings? ?? new ResourceSerializationSettings();

            this.Serialize(info, settings);
        }