protected override bool OnRead <T>(IXReadOperation reader, XType <T> xType, XAttribute attribute, Func <object, bool> assign, XObjectArgs args) { Type type = typeof(T); if (Identifier.CanId(type)) { string text = attribute.Value; if (!string.IsNullOrWhiteSpace(text) && text.Length > 0) { reader.AddTask(this, () => { if (referenceObjects.TryGetValue(text, out object refObject)) { if (refObject == null || refObject.GetType() == type) { return(assign(refObject)); } else { throw new InvalidOperationException( $"Possible collision: the reference object with ID {text} was of expected type {type.Name}, " + $"but that ID resolved to an object of type {refObject.GetType().Name}."); } } return(false); }); return(true); } } return(false); }
protected override void OnSubmit(IXReadOperation reader, object obj) { if (obj != null && Identifier.CanId(obj.GetType())) { object id = Identifier.GetId(obj); if (id != null) { Submit(id, obj); } else { reader.AddTask(this, () => { object id2 = Identifier.GetId(obj); if (id2 != null) { Submit(id2, obj); return(true); } return(false); }); } } }
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 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 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); }