/// <summary>
        /// This method reads the specified element.
        /// </summary>
        /// <param name="pObjectToInitialize">The object to initialize</param>
        /// <param name="pElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The initialized object if the input object is valid.</returns>
        public override object Read(object pObjectToInitialize, System.Xml.Linq.XElement pElement, IXSerializationContext pSerializationContext)
        {
            IXTemplate lTemplate = pObjectToInitialize as IXTemplate;

            if (lTemplate != null)
            {
                Type lTemplatedObjectType = pSerializationContext.ResolveType(pElement.Element(XConstants.TYPE_TAG));
                if (lTemplatedObjectType != null)
                {
                    IXSerializationContract lContract = pSerializationContext.SelectContract(null, lTemplatedObjectType);
                    object lTemplatedObject           = null;
                    if (lContract != null)
                    {
                        lTemplatedObject = lContract.Read(lTemplatedObject, pElement, pSerializationContext);
                    }

                    if (lTemplatedObject != null)
                    {
                        lTemplate.InitializeFrom(lTemplatedObject);
                    }
                }
            }

            return(pObjectToInitialize);
        }
        /// <summary>
        /// This method reads the specified element.
        /// </summary>
        /// <param name="pObjectToInitialize">The object to initialize</param>
        /// <param name="pParentElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The initialized object if the input object is valid.</returns>
        public override object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            if (pObjectToInitialize != null)
            {
                IDictionary lDictionary = pObjectToInitialize as IDictionary;
                if (lDictionary != null)
                {
                    foreach (XElement lChild in pParentElement.Elements(XConstants.ITEM_TAG))
                    {
                        IXSerializationContract lKeyContract = pSerializationContext.SelectContract(lChild.Element(XConstants.KEY_TAG), null, lDictionary.GetType().GetGenericArguments()[0], null);
                        object lKeyObject = null;
                        lKeyObject = lKeyContract.Read(lKeyObject, lChild.Element(XConstants.KEY_TAG), pSerializationContext);

                        Type     lValueType        = lDictionary.GetType().GetGenericArguments()[1];
                        XElement lValueTypeElement = lChild.Element(XConstants.TYPE_TAG);
                        if (lValueTypeElement != null)
                        {
                            lValueType = pSerializationContext.ResolveType(lValueTypeElement);
                        }

                        IXSerializationContract lValueContract = pSerializationContext.SelectContract(lChild.Element(XConstants.VALUE_TAG), null, lValueType, null);
                        object lValueObject = null;
                        lValueObject            = lValueContract.Read(lValueObject, lChild.Element(XConstants.VALUE_TAG), pSerializationContext);
                        lDictionary[lKeyObject] = lValueObject;
                    }
                }
            }
            return(pObjectToInitialize);
        }
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The created object.</returns>
        public virtual object Create(XElement pElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement   = pElement.Element(XConstants.TYPE_TAG);
            Type     lRetrievedType = null;
            string   lValueAsString = null;

            if (lTypeElement != null)
            {
                lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                XElement lValueElement = pElement.Element(XConstants.VALUE_TAG);
                if (lRetrievedType != null && lValueElement != null)
                {
                    lValueAsString = lValueElement.Value;
                }
            }
            else
            {
                lRetrievedType = this.mObjectType;
                lValueAsString = pElement.Value;
            }

            if (lRetrievedType != null && lValueAsString != null)
            {
                return(Enum.Parse(lRetrievedType, lValueAsString));
            }

            return(null);
        }
Esempio n. 4
0
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pParentElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>
        /// The created object.
        /// </returns>
        public object Create(XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG);

            if (lTypeElement != null)
            {
                Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                if (lRetrievedType != null)
                {
                    XElement lKeyElement = pParentElement.Descendants(XConstants.KEY_TAG).FirstOrDefault();
                    IXSerializationContract lKeyContract = pSerializationContext.SelectContract(lKeyElement, null, lRetrievedType.GetGenericArguments()[0], null);
                    object lKeyObject = null;
                    if (lKeyContract.NeedCreate)
                    {
                        lKeyObject = lKeyContract.Create(lKeyElement, pSerializationContext);
                    }
                    else
                    {
                        try
                        {
                            lKeyObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[0], true);
                        }
                        catch
                        {
                        }
                    }
                    lKeyObject = lKeyContract.Read(lKeyObject, lKeyElement, pSerializationContext);

                    XElement lValueElement = pParentElement.Descendants(XConstants.VALUE_TAG).FirstOrDefault();
                    IXSerializationContract lValueContract = pSerializationContext.SelectContract(lValueElement, null, lRetrievedType.GetGenericArguments()[1], null);
                    object lValueObject = null;
                    if (lValueContract.NeedCreate)
                    {
                        lValueObject = lValueContract.Create(lValueElement, pSerializationContext);
                    }
                    else
                    {
                        try
                        {
                            lValueObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[1], true);
                        }
                        catch
                        {
                        }
                    }
                    lValueObject = lValueContract.Read(lValueObject, lValueElement, pSerializationContext);
                    return(Activator.CreateInstance(lRetrievedType, new object[] { lKeyObject, lValueObject }));
                }
            }
            return(null);
        }
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pObjectElement">The object element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The created object.</returns>
        public virtual object Create(XElement pObjectElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement = pObjectElement.Element(XConstants.TYPE_TAG);

            if (lTypeElement != null)
            {
                Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                if (lRetrievedType != null)
                {
                    return(Activator.CreateInstance(lRetrievedType, true));
                }
            }
            return(null);
        }
        /// <summary>
        /// Attempt to retrieve the missing reference (last chance)
        /// </summary>
        /// <param name="pObjectToInitialize">The object to initialize</param>
        /// <param name="pParentElement"></param>
        /// <param name="pReference">The reference to look for.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The extracted object, null otherwise.</returns>
        private object AttemptReferenceRetrieval(object pObjectToInitialize, XElement pParentElement, int pReference, IXSerializationContext pSerializationContext)
        {
            object   lResult = null;
            XElement lXRoot  = this.GetRoot(pParentElement);

            if (lXRoot != null)
            {
                IEnumerable <XElement> lXElements = lXRoot.Descendants(pParentElement.Name);
                if (lXElements != null &&
                    lXElements.Any())
                {
                    foreach (XElement lXElement in lXElements)
                    {
                        XAttribute lXId = lXElement.Attribute(XConstants.ID_ATTRIBUTE);
                        if (lXId != null)
                        {
                            int lId = Convert.ToInt32(lXId.Value.Trim(), CultureInfo.InvariantCulture);
                            if (lId == pReference)
                            {
                                XElement lXType = lXElement.Element(XConstants.TYPE_TAG);
                                if (lXType != null)
                                {
                                    Type lType = pSerializationContext.ResolveType(lXType);
                                    IXSerializationContract lContract = pSerializationContext.SelectContract(lXElement, null, lType, pObjectToInitialize);
                                    if (lContract != null)
                                    {
                                        if (lContract.NeedCreate)
                                        {
                                            pObjectToInitialize = lContract.Create(lXElement, pSerializationContext);
                                        }

                                        lResult = lContract.Read(pObjectToInitialize, lXElement, pSerializationContext);
                                        if (lResult != null)
                                        {
                                            pSerializationContext.PushObject(lResult, pReference); // Cache it into ref by obj caches.
                                            pSerializationContext.PopObject();                     // But remove it from current object stack then to come back to previous current objects stack state.
                                        }
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }

            return(lResult);
        }
Esempio n. 7
0
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pParentElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>
        /// The created object.
        /// </returns>
        public object Create(XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG);

            if (lTypeElement != null)
            {
                Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                if (lRetrievedType != null)
                {
                    List <object> lParameters = new List <object>();
                    for (int lGenericIndex = 0; lGenericIndex < lRetrievedType.GetGenericArguments().Count(); lGenericIndex++)
                    {
                        string   lItemName    = XConstants.TUPLE_ITEM_TAG + (lGenericIndex + 1);
                        XElement lItemElement = pParentElement.Descendants(lItemName).FirstOrDefault();
                        IXSerializationContract lItemContract = pSerializationContext.SelectContract(lItemElement, null, lRetrievedType.GetGenericArguments()[lGenericIndex], null);
                        object lItemObject = null;
                        if (lItemContract.NeedCreate)
                        {
                            lItemObject = lItemContract.Create(lItemElement, pSerializationContext);
                        }
                        else
                        {
                            try
                            {
                                lItemObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[lGenericIndex], true);
                            }
                            catch
                            {
                            }
                        }
                        lItemObject = lItemContract.Read(lItemObject, lItemElement, pSerializationContext);
                        lParameters.Add(lItemObject);
                    }

                    return(Activator.CreateInstance(lRetrievedType, lParameters.ToArray()));
                }
            }


            return(null);
        }
        /// <summary>
        /// This method reads the specified element.
        /// </summary>
        /// <param name="pObjectToInitialize">The object to initialize</param>
        /// <param name="pParentElement">The parent element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The initialized object if the input object is valid.</returns>
        public override object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            if
            (pObjectToInitialize != null)
            {
                IList lList = pObjectToInitialize as IList;
                if
                (lList != null)
                {
                    foreach
                    (XElement lChild in pParentElement.Elements(XConstants.ITEM_TAG))
                    {
                        Type[] lInterfaces      = lList.GetType().GetInterfaces();
                        Type   lGenericListType = lInterfaces.FirstOrDefault(pType => pType.IsGenericType == true &&
                                                                             pType.GetGenericTypeDefinition() == typeof(IList <>));

                        Type     lItemType        = lGenericListType.GetGenericArguments()[0];
                        XElement lItemTypeElement = lChild.Element(XConstants.TYPE_TAG);
                        if
                        (lItemTypeElement != null)
                        {
                            lItemType = pSerializationContext.ResolveType(lItemTypeElement);
                        }

                        IXSerializationContract lContract = pSerializationContext.SelectContract(lChild, lItemType);
                        if
                        (lContract != null)
                        {
                            object lItem = null;
                            lItem = lContract.Read(lItem, lChild, pSerializationContext);
                            lList.Add(lItem);
                        }
                    }
                }
            }

            return(pObjectToInitialize);
        }
        /// <summary>
        /// THis method deserialized an X element in to an object.
        /// </summary>
        /// <param name="pObjectToInitialize"></param>
        /// <param name="pParentElement">The parent element to convert.</param>
        /// <param name="pSerializationContext">The context of serialization</param>
        /// <returns>The initialized object</returns>
        public object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement  = pParentElement.Element(XConstants.TYPE_TAG);
            XElement lValueElement = pParentElement.Element(XConstants.VALUE_TAG);

            if (lTypeElement != null && lValueElement != null)
            {
                Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                if (lRetrievedType != null)
                {
                    IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lValueElement, lRetrievedType.GetGenericArguments()[0]);
                    if (lSerializationContract != null)
                    {
                        Type   lValueType = lRetrievedType.GetGenericArguments()[0];
                        object lValue     = lValueType.DefaultValue();
                        lValue = lSerializationContract.Read(lValue, lValueElement, pSerializationContext);
                        return(Activator.CreateInstance(lRetrievedType, lValue));
                    }
                }
            }

            return(null);
        }
        /// <summary>
        /// THis method deserialized an X element in to an object.
        /// </summary>
        /// <param name="pObjectToInitialize"></param>
        /// <param name="pParentElement">The parent element to convert.</param>
        /// <param name="pSerializationContext">The context of serialization</param>
        /// <returns>The initialized object</returns>
        public object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG);
            XElement lValueElement = pParentElement.Element(XConstants.VALUE_TAG);
            if (lTypeElement != null && lValueElement != null)
            {
                Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                if (lRetrievedType != null)
                {
                    IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lValueElement, lRetrievedType.GetGenericArguments()[0]);
                    if (lSerializationContract != null)
                    {
                        Type lValueType = lRetrievedType.GetGenericArguments()[0];
                        object lValue = lValueType.DefaultValue();
                        lValue = lSerializationContract.Read(lValue, lValueElement, pSerializationContext);
                        return Activator.CreateInstance(lRetrievedType, lValue);
                    }
                }
            }

            return null;
        }
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The created object.</returns>
        public virtual object Create(XElement pElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement = pElement.Element(XConstants.TYPE_TAG);
            Type lRetrievedType = null;
            string lValueAsString = null;
            if (lTypeElement != null)
            {
                lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                XElement lValueElement = pElement.Element(XConstants.VALUE_TAG);
                if (lRetrievedType != null && lValueElement != null)
                {
                    lValueAsString = lValueElement.Value;
                }
            }
            else
            {
                lRetrievedType = this.mObjectType;
                lValueAsString = pElement.Value;
            }

            if (lRetrievedType != null && lValueAsString != null)
            {
                return Enum.Parse(lRetrievedType, lValueAsString);
            }

            return null;
        }
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pParentElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>
        /// The created object.
        /// </returns>
        public object Create(XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG);
            if (lTypeElement != null)
            {
                Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                if (lRetrievedType != null)
                {
                    XElement lKeyElement = pParentElement.Descendants(XConstants.KEY_TAG).FirstOrDefault();
                    IXSerializationContract lKeyContract = pSerializationContext.SelectContract(lKeyElement, null, lRetrievedType.GetGenericArguments()[0], null);
                    object lKeyObject = null;
                    if (lKeyContract.NeedCreate)
                    {
                        lKeyObject = lKeyContract.Create(lKeyElement, pSerializationContext);
                    }
                    else
                    {
                        try
                        {
                            lKeyObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[0], true);
                        }
                        catch
                        {
                        }

                    }
                    lKeyObject = lKeyContract.Read(lKeyObject, lKeyElement, pSerializationContext);

                    XElement lValueElement = pParentElement.Descendants(XConstants.VALUE_TAG).FirstOrDefault();
                    IXSerializationContract lValueContract = pSerializationContext.SelectContract(lValueElement, null, lRetrievedType.GetGenericArguments()[1], null);
                    object lValueObject = null;
                    if (lValueContract.NeedCreate)
                    {
                        lValueObject = lValueContract.Create(lValueElement, pSerializationContext);
                    }
                    else
                    {
                        try
                        {
                            lValueObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[1], true);
                        }
                        catch
                        {
                        }

                    }
                    lValueObject = lValueContract.Read(lValueObject, lValueElement, pSerializationContext);
                    return Activator.CreateInstance(lRetrievedType, new object[] {lKeyObject, lValueObject});
                }
            }
            return null;
        }
Esempio n. 13
0
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pParentElement">The element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>
        /// The created object.
        /// </returns>
        public object Create(XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XElement lTypeElement = pParentElement.Element(XConstants.TYPE_TAG);
            if (lTypeElement != null)
            {
                Type lRetrievedType = pSerializationContext.ResolveType(lTypeElement);
                if (lRetrievedType != null)
                {
                    List<object> lParameters = new List<object>();
                    for (int lGenericIndex = 0; lGenericIndex < lRetrievedType.GetGenericArguments().Count(); lGenericIndex++)
                    {
                        string lItemName = XConstants.ITEM_TAG + (lGenericIndex + 1);
                        XElement lItemElement = pParentElement.Descendants(lItemName).FirstOrDefault();
                        IXSerializationContract lItemContract = pSerializationContext.SelectContract(lItemElement, null, lRetrievedType.GetGenericArguments()[lGenericIndex], null);
                        object lItemObject = null;
                        if (lItemContract.NeedCreate)
                        {
                            lItemObject = lItemContract.Create(lItemElement, pSerializationContext);
                        }
                        else
                        {
                            try
                            {
                                lItemObject = Activator.CreateInstance(lRetrievedType.GetGenericArguments()[lGenericIndex], true);
                            }
                            catch
                            {
                            }

                        }
                        lItemObject = lItemContract.Read(lItemObject, lItemElement, pSerializationContext);
                        lParameters.Add(lItemObject);
                    }

                    return Activator.CreateInstance(lRetrievedType, lParameters.ToArray());
                }
            }

            return null;
        }