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 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); }