예제 #1
0
        private static object ReflectIn(StageElement element)
        {
            object obj;
            object obj1;
            object obj2;
            string str = element.AttributeValueOrDefault <string>("type", null);

            if (str == null)
            {
                throw new SerializationException("Invalid structure detected, missing type info.");
            }
            Type type = Type.GetType(str, true);

            try
            {
                obj = Activator.CreateInstance(type, true);
            }
            catch (MissingMethodException missingMethodException1)
            {
                MissingMethodException missingMethodException = missingMethodException1;
                throw new SerializationException(string.Format("Unable to create type {0}, ensure it has a parameterless constructor", type.Name), missingMethodException);
            }
            IInitializeAfterDeserialization initializeAfterDeserialization = obj as IInitializeAfterDeserialization;

            if (initializeAfterDeserialization != null)
            {
                if (SerializationMaster._requiresInit == null)
                {
                    throw new InvalidOperationException("An entity requires initialization but was unable to register, call UnstageAndInitialize instead.");
                }
                SerializationMaster._requiresInit.Add(initializeAfterDeserialization);
            }
            foreach (PropertyInfo serializedProperty in SerializationMaster.GetSerializedProperties(type))
            {
                StageItem stageItem = element.Item(serializedProperty.Name);
                if (stageItem == null || !SerializationMaster.TryUnstage(stageItem, serializedProperty.PropertyType, out obj1))
                {
                    continue;
                }
                serializedProperty.SetValue(obj, obj1, null);
            }
            foreach (FieldInfo serializedField in SerializationMaster.GetSerializedFields(type))
            {
                StageItem stageItem1 = element.Item(serializedField.Name);
                if (stageItem1 == null || !SerializationMaster.TryUnstage(stageItem1, serializedField.FieldType, out obj2))
                {
                    continue;
                }
                serializedField.SetValue(obj, obj2);
            }
            return(obj);
        }
        /// <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);
            }
        }
예제 #3
0
 public static T ValueOrDefault <T>(this StageElement element, string itemName, T defaultValue = null)
 {
     if (element == null)
     {
         return(defaultValue);
     }
     return(element.Item(itemName).ValueOrDefault <T>(defaultValue));
 }
예제 #4
0
 public static T Value <T>(this StageElement element, string itemName)
 {
     if (element != null)
     {
         StageItem stageItem = element.Item(itemName);
         if (stageItem != null)
         {
             return(stageItem.ValueOrDefault <T>(default(T)));
         }
     }
     throw new ArgumentException(string.Concat("No item by that name was found: ", itemName));
 }
        /// <summary>
        /// Gets the converted value of the specified item.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="itemName">Name of the value.</param>
        /// <returns>The value of the item.</returns>
        /// <exception cref="System.ArgumentException">If no value item by that name was found.</exception>
        public static T Value <T>(this StageElement element, string itemName)
        {
            if (element != null)
            {
                var item = element.Item(itemName);
                if (item != null)
                {
                    return(ValueOrDefault <T>(item));
                }
            }

            throw new ArgumentException("No item by that name was found: " + itemName);
        }
예제 #6
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);
        }
예제 #7
0
        private static object ReflectIn(StageElement element)
        {
            var typeName = element.AttributeValueOrDefault <string>("type", null);

            if (typeName == null)
            {
                throw new SerializationException("Invalid structure detected, missing type info.");
            }

            var    itemType = Type.GetType(typeName, true);
            object instance;

            try
            {
                instance = Activator.CreateInstance(itemType, true);
            }
            catch (MissingMethodException mme)
            {
                throw new SerializationException(string.Format("Unable to create type {0}, ensure it has a parameterless constructor", itemType.Name), mme);
            }

            var forInit = instance as IInitializeAfterDeserialization;

            if (forInit != null)
            {
                if (_requiresInit == null)
                {
                    throw new InvalidOperationException("An entity requires initialization but was unable to register, call UnstageAndInitialize instead.");
                }

                _requiresInit.Add(forInit);
            }

            var properties = GetSerializedProperties(itemType);

            foreach (var p in properties)
            {
                var member = element.Item(p.Name);
                if (member == null)
                {
                    continue;
                }

                object val;
                if (TryUnstage(member, p.PropertyType, out val))
                {
                    p.SetValue(instance, val, null);
                }
            }

            var fields = GetSerializedFields(itemType);

            foreach (var f in fields)
            {
                var member = element.Item(f.Name);
                if (member == null)
                {
                    continue;
                }

                object val;
                if (TryUnstage(member, f.FieldType, out val))
                {
                    f.SetValue(instance, val);
                }
            }

            return(instance);
        }