/// <summary> /// Unstages and initialize the staged item. /// </summary> /// <typeparam name="T">The type of the item to construct from the staged form.</typeparam> /// <param name="item">The staged item.</param> /// <returns>The item.</returns> public static T UnstageAndInitialize <T>(StageItem item) { if (_initBuffer == null) { _initBuffer = new List <IInitializeAfterDeserialization>(); } else { _initBuffer.Clear(); } var root = Unstage <T>(item, _initBuffer); if (_initBuffer.Count > 0) { foreach (var doInit in _initBuffer) { doInit.Initialize(root); } _initBuffer.Clear(); } return(root); }
/// <summary> /// Gets all descendants of a particular type. /// </summary> /// <typeparam name="T">The type of descendant.</typeparam> /// <returns>All descendants of the specified type.</returns> public IEnumerable <T> Descendants <T>() where T : StageItem { StageItem current = this; StageContainer curElement = this; while (true) { if (curElement == null || curElement._tailChild == null) { while (current != this && current == current.parent._tailChild) { current = current.parent; } if (current == this) { break; } current = current.next; } else { current = curElement._tailChild.next; } var el = current as T; if (el != null) { yield return(el); } curElement = current as StageContainer; } }
internal override void Remove(StageItem item) { if (item == null) { return; } if (item.parent != this) { throw new ArgumentException("Cannot remove item not belonging to this element.", "item"); } if (item.next != item) { StageItem stageItem = this._tailChild; while (stageItem.next != item) { stageItem = stageItem.next; } stageItem.next = item.next; if (item == this._tailChild) { this._tailChild = stageItem; } } else { this._tailChild = null; } item.next = null; item.parent = null; }
/// <summary> /// Gets all descendant items with the specified name. /// </summary> /// <returns>All descendant items with the specified name.</returns> public IEnumerable <StageItem> Descendants(string name) { StageItem current = this; StageContainer curElement = this; while (true) { if (curElement == null || curElement._tailChild == null) { while (current != this && current == current.parent._tailChild) { current = current.parent; } if (current == this) { break; } current = current.next; } else { current = curElement._tailChild.next; } if (current.name == name) { yield return(current); } curElement = current as StageContainer; } }
public IEnumerable <StageItem> Descendants() { StageContainer stageContainer = null; StageItem stageItem = stageContainer; StageContainer stageContainer1 = stageContainer; while (true) { if (stageContainer1 != null) { if (stageContainer1._tailChild == null) { goto Label2; } stageItem = stageContainer1._tailChild.next; goto Label0; } Label2: while (stageItem != stageContainer && stageItem == stageItem.parent._tailChild) { stageItem = stageItem.parent; } if (stageItem == stageContainer) { break; } stageItem = stageItem.next; Label0: yield return(stageItem); stageContainer1 = stageItem as StageContainer; } yield break; goto Label2; }
internal override void Remove(StageItem item) { if (item == null) { return; } if (item.parent != this) { throw new ArgumentException("Cannot remove item not belonging to this element.", "item"); } if (item.next == item) { _tailChild = null; } else { var current = _tailChild; while (current.next != item) { current = current.next; } current.next = item.next; if (item == _tailChild) { _tailChild = current; } } item.next = null; item.parent = null; }
public static object Unstage(StageItem item, Type targetType) { if (item is StageNull) { return(null); } IStager stager = SerializationMaster.GetStager(targetType); if (stager != null) { return(stager.UnstageValue(item, targetType)); } StageValue stageValue = item as StageValue; if (stageValue == null) { StageElement stageElement = item as StageElement; if (stageElement == null) { throw new SerializationException(string.Concat("Unable to unstage, the element is not supported: ", targetType.Name)); } return(SerializationMaster.ReflectIn(stageElement)); } IValueConverter converter = SerializationMaster.GetConverter(targetType); if (converter == null) { throw new SerializationException(string.Concat("Unable to unstage, no converter or stager was found for type: ", targetType.Name)); } return(converter.FromString(stageValue.@value, targetType)); }
public static T Unstage <T>(StageItem item, ICollection <IInitializeAfterDeserialization> requiresInit) { T t; if (SerializationMaster._requiresInit != null) { throw new InvalidOperationException("Generic overloads of Unstage cannot be called during a nested unstage operation."); } SerializationMaster._requiresInit = requiresInit; try { object obj = SerializationMaster.Unstage(item, typeof(T)); if (obj != null) { t = (T)obj; } else { t = default(T); t = t; } } finally { SerializationMaster._requiresInit = null; } return(t); }
/// <summary> /// Unstages the specified staged item. /// </summary> /// <typeparam name="T">The type of the item to construct from the staged form.</typeparam> /// <param name="item">The staged item.</param> /// <param name="requiresInit">A list that will be populated with references to all entities in the graph that require initialization.</param> /// <returns>The item.</returns> /// <exception cref="System.InvalidOperationException">If called during a nested unstage operation.</exception> public static T Unstage <T>(StageItem item, ICollection <IInitializeAfterDeserialization> requiresInit) { //We make use of a thread static var since we cannot pass it through the call hierarchy as some stagers may call methods on here as well. if (_requiresInit != null) { throw new InvalidOperationException("Generic overloads of Unstage cannot be called during a nested unstage operation."); } _requiresInit = requiresInit; try { var result = Unstage(item, typeof(T)); if (result == null) { return(default(T)); } return((T)result); } finally { _requiresInit = null; } }
public static T Deserialize <T>(string data, ICollection <IInitializeAfterDeserialization> requiresInit) { SerializationMaster.EnsureInit(); StageItem stageItem = SerializationMaster._serializer.Deserialize(data); if (stageItem == null) { return(default(T)); } return(SerializationMaster.Unstage <T>(stageItem, requiresInit)); }
public static T Deserialize <T>(string data) { SerializationMaster.EnsureInit(); StageItem stageItem = SerializationMaster._serializer.Deserialize(data); if (stageItem != null) { return(SerializationMaster.UnstageAndInitialize <T>(stageItem)); } return(default(T)); }
public static string Serialize <T>(T item, bool pretty = false) { SerializationMaster.EnsureInit(); StageItem stageItem = SerializationMaster.Stage(typeof(T).Name, item); if (stageItem == null) { return(string.Empty); } return(SerializationMaster._serializer.Serialize(stageItem, pretty)); }
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)); }
public static T ValueOrDefault <T>(this StageItem item, T defaultValue = null) { if (item == null || item is StageNull) { return(defaultValue); } if (item is StageContainer) { return(SerializationMaster.UnstageAndInitialize <T>(item)); } return(SerializationMaster.FromString <T>(((StageValue)item).@value)); }
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); }
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); }
public IEnumerable <StageItem> Items() { StageContainer stageContainer = null; if (stageContainer._tailChild == null) { yield break; } StageItem stageItem = stageContainer._tailChild; do { stageItem = stageItem.next; yield return(stageItem); }while (stageItem != stageContainer._tailChild); }
public StageItem Item(string name) { if (this._tailChild == null) { return(null); } StageItem stageItem = this._tailChild; do { if (stageItem.name == name) { return(stageItem); } stageItem = stageItem.next; }while (stageItem != this._tailChild); return(null); }
public IEnumerable <StageItem> Items(string name) { StageElement stageElement = null; if (stageElement._tailChild == null) { yield break; } StageItem stageItem = stageElement._tailChild; do { stageItem = stageItem.next; if (stageItem.name != name) { continue; } yield return(stageItem); }while (stageItem != stageElement._tailChild); }
public IEnumerable <StageElement> Elements(string name) { StageElement stageElement = null; if (stageElement._tailChild == null) { yield break; } StageItem stageItem = stageElement._tailChild; do { stageItem = stageItem.next; StageElement stageElement1 = stageItem as StageElement; if (stageElement1 == null || !(stageElement1.name == name)) { continue; } yield return(stageElement1); }while (stageItem != stageElement._tailChild); }
/// <summary> /// Adds the specified item. /// </summary> /// <param name="item">The item.</param> public override void Add(StageItem item) { if (item == null) { return; } if (_tailChild == null) { _tailChild = item; _tailChild.next = _tailChild; } else { item.next = _tailChild.next; _tailChild.next = item; _tailChild = item; } item.parent = this; }
public IEnumerable <StageElement> Elements() { StageContainer stageContainer = null; if (stageContainer._tailChild == null) { yield break; } StageItem stageItem = stageContainer._tailChild; do { stageItem = stageItem.next; StageElement stageElement = stageItem as StageElement; if (stageElement == null) { continue; } yield return(stageElement); }while (stageItem != stageContainer._tailChild); }
public static T UnstageAndInitialize <T>(StageItem item) { if (SerializationMaster._initBuffer != null) { SerializationMaster._initBuffer.Clear(); } else { SerializationMaster._initBuffer = new List <IInitializeAfterDeserialization>(); } T t = SerializationMaster.Unstage <T>(item, SerializationMaster._initBuffer); if (SerializationMaster._initBuffer.Count > 0) { foreach (IInitializeAfterDeserialization initializeAfterDeserialization in SerializationMaster._initBuffer) { initializeAfterDeserialization.Initialize(t); } SerializationMaster._initBuffer.Clear(); } return(t); }
public IEnumerable <T> Descendants <T>() where T : StageItem { StageContainer stageContainer = null; StageItem stageItem = stageContainer; StageContainer stageContainer1 = stageContainer; while (true) { if (stageContainer1 != null) { if (stageContainer1._tailChild == null) { goto Label2; } stageItem = stageContainer1._tailChild.next; goto Label0; } Label2: while (stageItem != stageContainer && stageItem == stageItem.parent._tailChild) { stageItem = stageItem.parent; } if (stageItem == stageContainer) { break; } stageItem = stageItem.next; Label0: T t = (T)(stageItem as T); if (t != null) { yield return(t); } stageContainer1 = stageItem as StageContainer; } yield break; goto Label2; }
/// <summary> /// Unstages the specified staged item. This is intended for use by <see cref="IStager"/>s. /// </summary> /// <param name="item">The staged item.</param> /// <param name="targetType">Type of the item.</param> /// <returns>The unstaged value</returns> /// <exception cref="System.Runtime.Serialization.SerializationException"> /// If no <see cref="IStager"/> or <see cref="IValueConverter"/> matching <paramref name="targetType"/> was found /// or /// If the element is not supported, e.g. a custom derivative of <see cref="StageItem"/>. /// </exception> public static object Unstage(StageItem item, Type targetType) { if (item is StageNull) { return(null); } var stager = GetStager(targetType); if (stager != null) { return(stager.UnstageValue(item, targetType)); } var valueItem = item as StageValue; if (valueItem != null) { var converter = GetConverter(targetType); if (converter != null) { return(converter.FromString(valueItem.value, targetType)); } else { throw new SerializationException("Unable to unstage, no converter or stager was found for type: " + targetType.Name); } } var elementItem = item as StageElement; if (elementItem == null) { throw new SerializationException("Unable to unstage, the element is not supported: " + targetType.Name); } return(ReflectIn(elementItem)); }
private static bool TryUnstage(StageItem item, Type targetType, out object value) { value = Unstage(item, targetType); return(value != null); }