protected override void OnBuild(XType <KeyValuePair <TKey, TValue> > xType, IXReadOperation reader, XElement element, ObjectBuilder <KeyValuePair <TKey, TValue> > objectBuilder, XObjectArgs args) { XName keyName = XComponents.Component <XAutoCollections>().KeyName, valueName = XComponents.Component <XAutoCollections>().ValueName; XElement keyElement = element.Element(keyName); if (keyElement == null) { return; } bool foundKey = false, foundValue = false; TKey key = default; TValue value = default; reader.Read <TKey>(keyElement, x => { key = x; return(foundKey = true); }, XObjectArgs.DefaultIgnoreElementName); XElement valueElement = element.Element(valueName); if (valueElement != null) { reader.Read <TValue>(valueElement, x => { value = x; return(foundValue = true); }, XObjectArgs.DefaultIgnoreElementName); } else { foundValue = true; } reader.AddTask(this, () => { if (foundKey && foundValue) { objectBuilder.Object = new KeyValuePair <TKey, TValue>(key, value); return(true); } return(false); }); }
protected override void OnBuild(XType <T[]> xType, IXReadOperation reader, XElement element, ObjectBuilder <T[]> objectBuilder) { if (!element.HasElements) { return; } IEnumerable <XElement> itemElements = ItemsAsElements ? element.Elements() : element.Elements(ItemName); T[] array = new T[itemElements.Count()]; if (array.Length > 0) { int i = 0; foreach (XElement subElement in itemElements) { int idx = i++; reader.Read <T>(subElement, x => { array[i] = x; return(true); }, ReaderHints.IgnoreElementName); } } }
protected override void OnBuild(IXReadOperation reader, XElement element, ObjectBuilder <ReadOnlyDictionary <TKey, TValue> > objectBuilder, XObjectArgs args) => reader.Read <Dictionary <TKey, TValue> >(element, x => { objectBuilder.Object = new ReadOnlyDictionary <TKey, TValue>(x); return(true); }, args ?? XObjectArgs.DefaultIgnoreElementName);
protected override void OnBuild(IXReadOperation reader, XElement element, ObjectBuilder <LinkedListNode <T> > objectBuilder, XObjectArgs args) => reader.Read <T>(element, x => { objectBuilder.Object = new LinkedListNode <T>(x); return(true); }, args ?? XObjectArgs.DefaultIgnoreElementName);
protected override void OnBuild(IXReadOperation reader, XElement element, ObjectBuilder <T[, ]> objectBuilder, XObjectArgs args) { // Read as a jagged array T[][] jagged = null; reader.Read <T[][]>(element, x => { jagged = x; return(true); }); // Convert to a multidimensional array once read reader.AddTask(this, () => { if (jagged == null) { return(false); } int lb0 = jagged.GetLowerBound(0), n0 = jagged.Length; int lb1, n1; if (n0 > 0) { lb1 = jagged[0].GetLowerBound(0); n1 = jagged[0].Length; } else { lb1 = 0; n1 = 0; } objectBuilder.Object = (T[, ])Array.CreateInstance(typeof(T), new int[2] { n0, n1 }, new int[2] { lb0, lb1 }); for (int i = lb0, I = lb0 + n0; i < I; i++) { for (int j = lb1, J = lb1 + n1; j < J; j++) { objectBuilder.Object[i, j] = jagged[i][j]; } } return(true); }); }
protected override bool OnRead <T>(IXReadOperation reader, XType <T> xType, XElement element, Func <object, bool> assign, XObjectArgs args) { Type type = typeof(T); // A serialized reference has no attributes, no elements, some text, is of a type that can be ID'd, // and not of a type that has a registered XTexter string value = element.Value; if (!element.HasAttributes && !element.HasElements && !string.IsNullOrEmpty(value) && xType.Component <XTexter <T> >() == null && Identifier.CanId(type, out Type idType)) { bool idFound = false; object id = null; reader.Read(element, idType, x => { idFound = true; if (!Identifier.KeyComparer.Equals(x, ReflectionTools.GetDefaultValue(idType))) { id = x; } return(true); }, XObjectArgs.DefaultIgnoreElementName); // Schedule a task to assign the object if it shows up in the dictionary reader.AddTask(this, () => { if (!idFound) { return(false); } if (id == null) { return(true); } if (referenceObjects.TryGetValue(id, out object refObject)) { if (refObject == null || type == refObject.GetType()) { return(assign(refObject)); } else { throw new InvalidOperationException( $"Possible collision: the reference object with ID {id} was of expected type {type.Name}, " + $"but that ID resolved to an object of type {refObject.GetType().Name}."); } } return(false); }); return(true); } return(false); }