示例#1
0
        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 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);
        }
示例#3
0
        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);
        }