/// <summary>
        /// Sets the value of a text element. If it does not exist it is created.
        /// If multiple identically named items exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the item.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="removeIfNullOrEmpty">If <c>true</c> and <para<paramref name="value"/> is <c>null</c> or empty, the item is removed rather than converted to a <see cref="StageNull"/></param>
        public static void SetTextValue(this StageElement parent, string name, string value, bool removeIfNullOrEmpty = true)
        {
            bool remove = (removeIfNullOrEmpty && string.IsNullOrEmpty(value));

            var item = parent.Item(name);

            if (item == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }

                return;
            }

            if (item is StageAttribute)
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }

            var nullItem = item as StageNull;

            if (item != null)
            {
                if (value != null)
                {
                    nullItem.Remove();
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }

                return;
            }

            var valueItem = item as StageValue;

            if (item == null)
            {
                throw new InvalidOperationException("Only value elements can be set using this method.");
            }

            if (remove)
            {
                item.Remove();
            }
            else if (value == null)
            {
                item.Remove();
                parent.Add(new StageNull(name));
            }
            else if (!valueItem.isText)
            {
                throw new InvalidOperationException("Cannot set a text value on a non-text value item.");
            }
            else
            {
                valueItem.value = SerializationMaster.ToString(value);
            }
        }
Esempio n. 2
0
        private static StageElement ReflectOut(string elementName, object item)
        {
            var itemType  = item.GetType();
            var nameSplit = itemType.AssemblyQualifiedName.Split(',');
            var element   = new StageElement(elementName, new StageAttribute("type", string.Concat(nameSplit[0], ",", nameSplit[1]), true));

            var properties = from p in itemType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                             let attrib = p.GetAttribute <ApexSerializationAttribute>(true)
                                          where attrib != null && p.CanRead && p.CanWrite && (attrib.excludeMask & _excludeMask) == 0
                                          select new AIPropInfo
            {
                prop         = p,
                defaultValue = attrib.defaultValue
            };

            foreach (var p in properties)
            {
                var val = p.prop.GetValue(item, null);
                if (val != null && !val.Equals(p.defaultValue))
                {
                    StageItem propElement;
                    if (TryStage(p.prop.Name, val, out propElement))
                    {
                        element.Add(propElement);
                    }
                }
            }

            var fields = from f in itemType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                         let attrib = f.GetAttribute <ApexSerializationAttribute>(false)
                                      where attrib != null && (attrib.excludeMask & _excludeMask) == 0
                                      select new AIFieldInfo
            {
                field        = f,
                defaultValue = attrib.defaultValue
            };

            foreach (var f in fields)
            {
                var val = f.field.GetValue(item);
                if (val != null && !val.Equals(f.defaultValue))
                {
                    StageItem propElement;
                    if (TryStage(f.field.Name, val, out propElement))
                    {
                        element.Add(propElement);
                    }
                }
            }

            return(element);
        }
Esempio n. 3
0
        public static void SetValue(this StageElement parent, string name, object value, bool removeIfNull = true)
        {
            bool      flag      = (!removeIfNull ? false : value == null);
            StageItem stageItem = parent.Item(name);

            if (stageItem == null)
            {
                if (!flag)
                {
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }
                return;
            }
            if (stageItem is StageAttribute)
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }
            StageNull stageNull = stageItem as StageNull;

            if (stageItem != null)
            {
                if (value != null)
                {
                    stageNull.Remove();
                    parent.Add(SerializationMaster.ToStageValue(name, value));
                }
                return;
            }
            StageValue str = stageItem as StageValue;

            if (stageItem == null)
            {
                throw new InvalidOperationException("Only value elements can be set using this method.");
            }
            if (flag)
            {
                stageItem.Remove();
                return;
            }
            if (value == null)
            {
                stageItem.Remove();
                parent.Add(new StageNull(name));
                return;
            }
            if (str.isText && !(value is string))
            {
                throw new InvalidOperationException("Use SetTextValue to set text values.");
            }
            str.@value = SerializationMaster.ToString(value);
        }
Esempio n. 4
0
        private static StageElement ReflectOut(string elementName, object item)
        {
            StageItem stageItem;
            StageItem stageItem1;
            Type      type = item.GetType();

            string[]     strArrays    = type.AssemblyQualifiedName.Split(new char[] { ',' });
            StageElement stageElement = new StageElement(elementName, new StageItem[] { new StageAttribute("type", string.Concat(strArrays[0], ",", strArrays[1]), true) });

            foreach (SerializationMaster.AIPropInfo aIPropInfo in (
                         from p in (IEnumerable <PropertyInfo>)type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                         select new { p = p, attrib = p.GetAttribute <ApexSerializationAttribute>(true) }).Where((argument0) => {
                if (argument0.attrib == null || !argument0.p.CanRead)
                {
                    return(false);
                }
                return(argument0.p.CanWrite);
            }).Select((argument1) => new SerializationMaster.AIPropInfo()
            {
                prop = argument1.p,
                defaultValue = argument1.attrib.defaultValue
            }))
            {
                object value = aIPropInfo.prop.GetValue(item, null);
                if (value == null || value.Equals(aIPropInfo.defaultValue) || !SerializationMaster.TryStage(aIPropInfo.prop.Name, value, out stageItem))
                {
                    continue;
                }
                stageElement.Add(stageItem);
            }
            foreach (SerializationMaster.AIFieldInfo aIFieldInfo in
                     from f in (IEnumerable <FieldInfo>)type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                     let attrib = f.GetAttribute <ApexSerializationAttribute>(false)
                                  where attrib != null
                                  select new SerializationMaster.AIFieldInfo()
            {
                field = f,
                defaultValue = attrib.defaultValue
            })
            {
                object obj = aIFieldInfo.field.GetValue(item);
                if (obj == null || obj.Equals(aIFieldInfo.defaultValue) || !SerializationMaster.TryStage(aIFieldInfo.field.Name, obj, out stageItem1))
                {
                    continue;
                }
                stageElement.Add(stageItem1);
            }
            return(stageElement);
        }
Esempio n. 5
0
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool flag;

            if (value == null)
            {
                flag = true;
            }
            else
            {
                flag = (!removeIfEmpty ? false : string.IsNullOrEmpty(value));
            }
            bool           flag1 = flag;
            StageAttribute str   = parent.Attribute(name);

            if (str != null)
            {
                if (flag1)
                {
                    str.Remove();
                    return;
                }
                if (!str.isText)
                {
                    throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag1)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }
        /// <summary>
        /// Sets the value of a text attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="removeIfEmpty">if set to <c>true</c> the attribute is removed if the <paramref name="value"/> is empty. The attribute is always removed if <paramref name="value"/> is <c>null</c></param>
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool remove = (value == null) || (removeIfEmpty && string.IsNullOrEmpty(value));

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (!attrib.isText)
            {
                throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
        /// <summary>
        /// Sets the value of an attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool remove = (value == null);

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (attrib.isText && !(value is string))
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
Esempio n. 8
0
 public static void AddAttribute(this StageElement parent, string name, object value, bool onlyIfNotNull = true)
 {
     if (onlyIfNotNull && value == null)
     {
         return;
     }
     parent.Add(SerializationMaster.ToStageAttribute(name, value));
 }
Esempio n. 9
0
 public static void AddTextAttribute(this StageElement parent, string name, string value, bool onlyIfNotNullOrEmpty = true)
 {
     if (onlyIfNotNullOrEmpty && string.IsNullOrEmpty(value))
     {
         return;
     }
     parent.Add(SerializationMaster.ToStageAttribute(name, value));
 }
Esempio n. 10
0
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool           flag = value == null;
            StageAttribute str  = parent.Attribute(name);

            if (str != null)
            {
                if (flag)
                {
                    str.Remove();
                    return;
                }
                if (str.isText && !(value is string))
                {
                    throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }