protected override bool OnWrite <T>(IXWriteOperation writer, T obj, XAttribute attribute, XObjectArgs args) { if (obj != null && Identifier.CanId(obj.GetType())) { object id = Identifier.GetId(obj); if (id != null && referenceObjects.ContainsKey(id)) { _ = writer.WriteTo(attribute, id); return(true); } } return(false); }
protected override bool OnWrite(XType <DictionaryEntry> xType, IXWriteOperation writer, DictionaryEntry obj, XElement element, XObjectArgs args) { XName keyName = XComponents.Component <XAutoCollections>().KeyName, valueName = XComponents.Component <XAutoCollections>().ValueName; if (obj.Key != null) { element.Add(writer.WriteTo(new XElement(keyName), obj.Key)); if (obj.Value != default) { element.Add(writer.WriteTo(new XElement(valueName), obj.Value)); } } return(true); }
protected override bool OnWrite(IXWriteOperation writer, T[,] obj, XElement element, XObjectArgs args) { // Get lower bound and length int lb = obj.GetLowerBound(0), n = obj.GetLength(0); int[] lb1 = new int[1] { obj.GetLowerBound(1) }, n1 = new int[1] { obj.GetLength(1) }; if (lb != 0) { element.SetAttributeValue(XComponents.Component <XAutoCollections>().ArrayLowerBoundName, XmlTools.Write(lb)); } // Copy each second rank into a new array and write them as 1-dimensional arrays for (int i = lb, I = lb + n; i < I; i++) { T[] dim1 = (T[])Array.CreateInstance(typeof(T), n1, lb1); for (int j = lb1[0], J = n1[0]; j < J; j++) { dim1[j] = obj[i, j]; } XElement li = writer.WriteTo(new XElement(ItemName), dim1); if (lb1[0] != 0) { li.SetAttributeValue(XComponents.Component <XAutoCollections>().ArrayLowerBoundName, XmlTools.Write(lb1[0])); } element.Add(li); } return(true); }
protected override void OnSubmit(IXWriteOperation writer, object obj) { if (obj != null && Identifier.CanId(obj.GetType())) { object id = Identifier.GetId(obj); if (id != null) { if (referenceObjects.TryGetValue(id, out object existing)) { if (!Equals(existing, obj)) { throw new InvalidOperationException( $"Reference collision on ID '{id}' between objects of type " + $"{existing.GetType().Name} and {obj.GetType().Name}."); } } else { referenceObjects.Add(id, obj); } } } }
internal void Submit(IXWriteOperation writer, object obj) => OnSubmit(writer, obj);
internal bool Write <T>(IXWriteOperation writer, T obj, XAttribute attribute, XObjectArgs args) => OnWrite(writer, obj, attribute, args);
internal bool Write <T>(IXWriteOperation writer, T obj, XElement element, XObjectArgs args) => OnWrite(writer, obj, element, args);
/// <summary> /// Called whenever an object is submitted to the <see cref="XWriter"/>, which occurs either because the user /// has submitted a contextual object or because an object was successfully written to XML. /// </summary> /// <param name="writer">An <see cref="IXWriteOperation"/> instance that exposes methods for writing XML /// using the active <see cref="XWriter"/>.</param> /// <param name="obj">The object that was submitted.</param> protected virtual void OnSubmit(IXWriteOperation writer, object obj) { }
/// <summary> /// Called when an <see cref="XAttribute"/> has been created with the intention of writing an <see cref="object"/> /// to it, but before any XML has been added by <see cref="XTypeComponent{T}"/>s. /// </summary> /// <param name="writer">An <see cref="IXWriteOperation"/> instance that exposes methods for writing XML /// using the active <see cref="XWriter"/>.</param> /// <param name="obj">The object to be written.</param> /// <param name="attribute">The <see cref="XAttribute"/> to which <paramref name="obj"/> is being written. The /// attribute will already have been assigned the correct <see cref="XName"/>.</param> /// <param name="args">Optional arguments to communicate to components how <paramref name="obj"/> should be /// written.</param> /// <returns>True if this <see cref="XWriterComponent"/> wrote the complete object to XML and all further /// processing of this element should end.</returns> protected virtual bool OnWrite <T>(IXWriteOperation writer, T obj, XAttribute attribute, XObjectArgs args) => false;
/// <summary> /// Called when an <see cref="XElement"/> has been created with the intention of writing an <see cref="object"/> /// to it, but before any XML has been added by <see cref="XTypeComponent{T}"/>s. /// </summary> /// <param name="writer">An <see cref="IXWriteOperation"/> instance that exposes methods for writing XML /// using the active <see cref="XWriter"/>.</param> /// <param name="obj">The object to be written.</param> /// <param name="element">The <see cref="XElement"/> to which <paramref name="obj"/> is being written. The /// element will already have been assigned the correct <see cref="XName"/>.</param> /// <param name="args">Optional arguments to communicate to components how <paramref name="obj"/> should be /// written.</param> /// <returns>True if this <see cref="XWriterComponent"/> wrote the complete object to XML and all further /// processing of this element should end.</returns> protected virtual bool OnWrite <T>(IXWriteOperation writer, T obj, XElement element, XObjectArgs args) => false;
protected override bool OnWrite(IXWriteOperation writer, LinkedListNode <T> obj, XElement element, XObjectArgs args) { _ = writer.WriteTo(element, obj.Value); return(true); }
internal bool Write(XType <T> xType, IXWriteOperation writer, T obj, XElement element, XObjectArgs args) => OnWrite(xType, writer, obj, element, args);
/// <summary> /// Implement this method to write an object of type <typeparamref name="T"/> to the given <see cref="XElement"/>. /// </summary> /// <param name="xType">The <see cref="XType{T}"/> instance from the current <see cref="XDomain"/>.</param> /// <param name="writer">An <see cref="IXWriteOperation"/> instance for serializing XML.</param> /// <param name="obj">The <typeparamref name="T"/> to be serialized.</param> /// <param name="element">The <see cref="XElement"/> to which <paramref name="obj"/> is being written.</param> /// <param name="args">Optional arguments with information about XML formatting.</param> protected abstract bool OnWrite(XType <T> xType, IXWriteOperation writer, T obj, XElement element, XObjectArgs args);
protected override bool OnWrite(XType <T> xType, IXWriteOperation writer, T obj, XElement element, XObjectArgs args) => writeMethod(xType, writer, obj, element, args);
internal bool Write(IXWriteOperation writer, T obj, XAttribute attribute, XObjectArgs args) => ForEachComponent(x => x.Write(writer, obj, attribute, args));
internal bool Write(IXWriteOperation writer, T obj, XElement element, XObjectArgs args) => ForEachComponent(x => x.Write(writer, obj, element, args));