示例#1
0
        /// <summary>
        /// This method serializes the object in to an XElement.
        /// </summary>
        /// <param name="pObject">The object to serialize.</param>
        /// <param name="pParentElement">The parent element.</param>
        /// <param name="pSerializationContext">The context of serialization</param>
        /// <returns>
        /// The modified parent.
        /// </returns>
        public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            try
            {
                int    lReference         = XConstants.NOT_YET_REFERENCED_OBJECT;
                string lExternalReference = string.Empty;

                if (pSerializationContext.ExternalReferenceResolver.HasExternalReference(pObject) && pSerializationContext.CurrentObject != null)
                {
                    lExternalReference = pSerializationContext.ExternalReferenceResolver.GetExternalReference(pObject);
                }

                bool lCanBeCurrent    = this.CanBeCurrentObject(pObject);
                bool lCanBeReferenced = this.CanBeInternallyReferenced(pObject);
                if (lCanBeCurrent)
                {
                    if (lCanBeReferenced)
                    {
                        lReference = pSerializationContext.GetObjectReference(pObject);
                    }
                    pSerializationContext.PushObject(pObject);
                }

                XElement lModifiedElement;
                if (string.IsNullOrEmpty(lExternalReference) == false)
                {
                    pParentElement.Add(new XComment("Serialized with XSerialization.Values.ExternalReferenceSerializationContract"));
                    lModifiedElement = new ExternalReferenceSerializationContract().Write(pObject, pParentElement, pSerializationContext);
                }
                else if (lReference != XConstants.NOT_YET_REFERENCED_OBJECT)
                {
                    pParentElement.Add(new XComment("Serialized with XSerialization.Values.InternalReferenceSerializationContract"));
                    lModifiedElement = new InternalReferenceSerializationContract().Write(pObject, pParentElement, pSerializationContext);
                }
                else
                {
                    pParentElement.Add(new XComment("Serialized with " + this.DecoratedContract.GetType().XmlFullname()));
                    lModifiedElement = this.DecoratedContract.Write(pObject, pParentElement, pSerializationContext);
                }

                if (lCanBeCurrent)
                {
                    pSerializationContext.PopObject();
                }
                return(lModifiedElement);
            }
            catch
            {
                IXmlLineInfo lInfo = pParentElement;
                pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty));
            }
            return(pParentElement);
        }
示例#2
0
        /// <summary>
        /// This method reads the specified element.
        /// </summary>
        /// <param name="pObjectToInitialize">The object to initialize (it is the property value of the parent object)</param>
        /// <param name="pParentElement">The parent element (it is the property name of the parent object).</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The initialized object if the input object is valid.</returns>
        public virtual object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            PropertyInfo lPropertyInfo = pObjectToInitialize as PropertyInfo;

            if (lPropertyInfo != null && pSerializationContext.CurrentObject != null)
            {
                if (pParentElement.Elements(lPropertyInfo.Name).Elements("Field").Any())
                {
                    XElement lFieldElement = pParentElement.Elements(lPropertyInfo.Name).Elements("Field").FirstOrDefault(pElement => pElement.Attribute("fieldName") != null && pElement.Attribute("fieldName").Value == this.TypedAttribute.FieldName);
                    Type     lFieldType;
                    object   lFieldObject = pSerializationContext.CurrentObject.GetFieldValue(this.TypedAttribute.FieldName, out lFieldType);
                    IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(null, lFieldType);
                    if (lSerializationContract != null)
                    {
                        object lReadFieldObject = lSerializationContract.Read(lFieldObject, lFieldElement, pSerializationContext);
                        pSerializationContext.CurrentObject.SetFieldValue(this.TypedAttribute.FieldName, lReadFieldObject);
                    }

                    // Check if a initial synchronisation method exits.
                    if (string.IsNullOrWhiteSpace(this.TypedAttribute.SyncFieldMethod) == false)
                    {
                        MethodInfo lSynchronizationMethod = pSerializationContext.CurrentObject.GetType().GetMethod(this.TypedAttribute.SyncFieldMethod, BindingFlags.NonPublic | BindingFlags.Instance);
                        if (lSynchronizationMethod != null)
                        {
                            lSynchronizationMethod.Invoke(pSerializationContext.CurrentObject, null);
                        }
                    }
                }
                else if (pParentElement.Elements(lPropertyInfo.Name).Any())
                {
                    object lValue = new InternalReferenceSerializationContract().Read(pObjectToInitialize, pParentElement.Elements(lPropertyInfo.Name).FirstOrDefault(), pSerializationContext);
                    pSerializationContext.CurrentObject.SetPropertyValue(lPropertyInfo.Name, lValue);
                }
            }
            else
            {
                IXmlLineInfo lInfo = pParentElement;
                pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, "Field serialization contract must be called with a property info and not null object as parameter"));
            }

            return(pObjectToInitialize);
        }