/// <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, XElement pElement, IXSerializationContext pSerializationContext) { if (pObjectToInitialize == null) { return(null); } if (pElement == null) { return(null); } UInt16 lValue = (UInt16)(pObjectToInitialize); try { lValue = Convert.ToUInt16(pElement.Value.Trim(), CultureInfo.InvariantCulture); } catch (FormatException) { IXmlLineInfo lInfo = pElement; pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } catch (OverflowException) { IXmlLineInfo lInfo = pElement; pSerializationContext.PushError(new XSerializationError(XErrorType.NumberOverflow, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } return(lValue); }
/// <summary> /// This method reads the specified object to initialize. /// </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</returns> public override object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { PropertyInfo lPropertyInfo = pObjectToInitialize as PropertyInfo; // Look for a sub-element with the good local name. // ReSharper disable once PossibleNullReferenceException IEnumerable<XElement> lElements = pParentElement.Elements(lPropertyInfo.Name); XElement lPropertyElement = lElements.FirstOrDefault(); if (lPropertyElement == null) { IEnumerable<XElement> lDescendants = pParentElement.Descendants(lPropertyInfo.Name); lPropertyElement = lDescendants.FirstOrDefault(); } if (lPropertyElement != null) { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lPropertyElement, lPropertyInfo.PropertyType); if (lSerializationContract != null) { object lReadObject = lSerializationContract.Read(lPropertyInfo.GetValue(pSerializationContext.CurrentObject, null), lPropertyElement, pSerializationContext); return pObjectToInitialize; } } return null; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject == null) { return(new SupportPriority(SupportLevel.Element, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method checks if the object type can be managed by the contract. /// </summary> /// <param name="pObjectType">The object type to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>A support priority</returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType.Name == typeof(KeyValuePair <,>).Name) { return(new SupportPriority(SupportLevel.Type, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject == null) { return new SupportPriority(SupportLevel.Element, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject is Attribute) { return(this.CanManage((Attribute)pObject, pSerializationContext)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method checks if the object type can be managed by the contract. /// </summary> /// <param name="pObjectType">The object type to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>A support priority</returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType.Name.StartsWith(typeof(Nullable).Name)) { return new SupportPriority(SupportLevel.Type, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject is PropertyInfo) { return this.CanManage(pObject as PropertyInfo, pSerializationContext); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// Determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns></returns> public override SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject != null) { return(this.CanManage(pObject.GetType(), pSerializationContext)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pPropertyInfo">The property info to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public SupportPriority CanManage(PropertyInfo pPropertyInfo, IXSerializationContext pSerializationContext) { if (pPropertyInfo.Name == this.PropertyName && this.DeclaringType.IsAssignableFrom(pPropertyInfo.DeclaringType) && this.PropertyType == pPropertyInfo.PropertyType) { return(new SupportPriority(SupportLevel.PropertyInfo, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method checks if the object type can be managed by the contract. /// </summary> /// <param name="pObjectType">The object type to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>A support priority</returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType.Name.StartsWith(typeof(Nullable).Name)) { return(new SupportPriority(SupportLevel.Type, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// Determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns></returns> public override SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (this.SupportedType.IsInstanceOfType(pObject)) { return(new SupportPriority(SupportLevel.Type, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this type can manage the specified object. /// </summary> /// <param name="pObjectType">The object type to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType == typeof(PropertyInfo)) { return(new SupportPriority(SupportLevel.Type, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The modified parent element</returns> public virtual XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { string lExternalReference = pSerializationContext.ExternalReferenceResolver.GetExternalReference(pObject); string lRelativePath = this.MakeRelativePath(pSerializationContext.CurrentDirectory.FullName, lExternalReference); pParentElement.SetAttributeValue(XConstants.EXTERNAL_REFERENCE_ATTRIBUTE, lRelativePath); return(pParentElement); }
/// <summary> /// This method determines whether this type can manage the specified object. /// </summary> /// <param name="pObjectType">The object type to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType == typeof(PropertyInfo)) { return new SupportPriority(SupportLevel.Type, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject is PropertyInfo) { return(this.CanManage(pObject as PropertyInfo, pSerializationContext)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject is Attribute) { return this.CanManage(pObject as Attribute, pSerializationContext); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method checks if the object type can be managed by the contract. /// </summary> /// <param name="pObjectType">The object type to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>A support priority</returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType.Name == typeof(KeyValuePair<,>).Name) { return new SupportPriority(SupportLevel.Type, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// Determines whether this instance can manage the specified object type. /// </summary> /// <param name="pObjectType">The object type.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns></returns> public override SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (this.SupportedType.IsAssignableFrom(pObjectType)) { return(new SupportPriority(SupportLevel.Interface, pObjectType.DistanceTo(this.SupportedType))); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { // Look for : <ParentElement ref="00001"><ParentElement> if (pParentElement.Attribute(XConstants.REFERENCE_ATTRIBUTE) != null && pParentElement.Name != XConstants.TYPE_TAG) { return(new SupportPriority(SupportLevel.Element, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method checks if the object can be managed by the contract. /// </summary> /// <param name="pParentElement">The element to manage.</param> /// <remarks>The object can be a type, a property info, ... </remarks> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>The support priority or SupportPriority.CANNOT_SUPPORT</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { if (pParentElement.Name == this.ElementName) { return new SupportPriority(SupportLevel.Element, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this type can manage the specified object. /// </summary> /// <param name="pObjectType">The object type to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType.IsEnum) { this.mObjectType = pObjectType; return(new SupportPriority(SupportLevel.Default, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pAttribute">The attribute to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(Attribute pAttribute, IXSerializationContext pSerializationContext) { if (this.SupportedAttribute.IsInstanceOfType(pAttribute)) { return new SupportPriority(SupportLevel.Attribute, pAttribute.GetType().DistanceTo(this.SupportedAttribute)); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { // Look for : <ParentElement><Null/><ParentElement> if (pParentElement.Element(XConstants.NULL_TAG) != null) { return new SupportPriority(SupportLevel.Element, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { // Look for : <ParentElement ref="00001"><ParentElement> if (pParentElement.Attribute(XConstants.REFERENCE_ATTRIBUTE) != null) { return new SupportPriority(SupportLevel.Element, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method checks if the object can be managed by the contract. /// </summary> /// <param name="pParentElement">The element to manage.</param> /// <remarks>The object can be a type, a property info, ... </remarks> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>The support priority or SupportPriority.CANNOT_SUPPORT</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { if (pParentElement.Name == this.ElementName) { return(new SupportPriority(SupportLevel.Element, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method checks if the object can be managed by the contract. /// </summary> /// <param name="pObject">The object to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>A support priority</returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject != null) { // ReSharper disable once OperatorIsCanBeUsed return(this.CanManage(pObject.GetType(), pSerializationContext)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { // Look for : <ParentElement><Null/><ParentElement> if (pParentElement.Element(XConstants.NULL_TAG) != null) { return(new SupportPriority(SupportLevel.Element, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this type can manage the specified object. /// </summary> /// <param name="pObjectType">The object type to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { if (pObjectType.IsEnum) { this.mObjectType = pObjectType; return new SupportPriority(SupportLevel.Default, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pAttribute">The attribute to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(Attribute pAttribute, IXSerializationContext pSerializationContext) { if (this.SupportedAttribute.IsInstanceOfType(pAttribute)) { return(new SupportPriority(SupportLevel.Attribute, pAttribute.GetType().DistanceTo(this.SupportedAttribute))); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified element. /// </summary> /// <param name="pElement">The element to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pElement, IXSerializationContext pSerializationContext) { Type lObjectType = AppDomain.CurrentDomain.GetTypeByFullName(pElement.Name.LocalName); if (lObjectType != null) { this.mObjectType = lObjectType; return this.CanManage(lObjectType, pSerializationContext); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method creates the specified element. /// </summary> /// <param name="pObjectElement">The element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The created object.</returns> public override object Create(XElement pObjectElement, IXSerializationContext pSerializationContext) { Type lRetrievedType = AppDomain.CurrentDomain.GetTypeByFullName(pObjectElement.Value); if (lRetrievedType != null) { return(Activator.CreateInstance(lRetrievedType, true)); } return(null); }
/// <summary> /// This method checks if the object can be managed by the contract. /// </summary> /// <param name="pObject">The object to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>A support priority</returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject != null) { // ReSharper disable once OperatorIsCanBeUsed return this.CanManage(pObject.GetType(), pSerializationContext); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { // Look for : <ParentElement><Null/><ParentElement> if (pParentElement.Attribute(XConstants.EXTERNAL_REFERENCE_ATTRIBUTE) != null) { this.mTargetElement = pParentElement; return new SupportPriority(SupportLevel.Element, 0); } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { // Look for : <ParentElement><Null/><ParentElement> if (pParentElement.Attribute(XConstants.EXTERNAL_REFERENCE_ATTRIBUTE) != null) { this.mTargetElement = pParentElement; return(new SupportPriority(SupportLevel.Element, 0)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pType">The type to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public override SupportPriority CanManage(Type pType, IXSerializationContext pSerializationContext) { SupportPriority lInitialPriority = base.CanManage(pType, pSerializationContext); if (lInitialPriority.Level == SupportLevel.NotSupported) { return(lInitialPriority); } return(new SupportPriority(SupportLevel.Default, lInitialPriority.SubPriority)); }
/// <summary> /// This method determines whether this instance can manage the specified element. /// </summary> /// <param name="pElement">The element to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(XElement pElement, IXSerializationContext pSerializationContext) { Type lObjectType = AppDomain.CurrentDomain.GetTypeByFullName(pElement.Name.LocalName); if (lObjectType != null) { this.mObjectType = lObjectType; return(this.CanManage(lObjectType, pSerializationContext)); } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The modified parent element</returns> public virtual XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); XElement lValuelement = new XElement(XConstants.VALUE_TAG); lValuelement.Value = pObject.ToString(); pParentElement.Add(lValuelement); return(pParentElement); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject != null) { if (pObject.GetType().IsEnum) { this.mObjectType = pObject.GetType(); return(new SupportPriority(SupportLevel.Type, 0)); } } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pType">The type to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public override SupportPriority CanManage(Type pType, IXSerializationContext pSerializationContext) { if (pType.IsPrimitive) { if (pType == this.SupportedType) { return(new SupportPriority(SupportLevel.Type, 0)); } } return(SupportPriority.CANNOT_SUPPORT); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public virtual SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject != null) { if (pObject.GetType().IsEnum) { this.mObjectType = pObject.GetType(); return new SupportPriority(SupportLevel.Type, 0); } } return SupportPriority.CANNOT_SUPPORT; }
/// <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); }
/// <summary> /// Reads the specified element. /// </summary> /// <param name="pObjectToInitialize"></param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns></returns> public object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { PropertyInfo lPropertyInfo = pObjectToInitialize as PropertyInfo; // ReSharper disable once PossibleNullReferenceException if (pParentElement.Element(lPropertyInfo.Name) != null) { object lValue = lPropertyInfo.GetValue(pSerializationContext.CurrentObject, null); XElement lPropertyElement = pParentElement.Element(lPropertyInfo.Name); return(this.SubContract.Read(lValue, lPropertyElement, pSerializationContext)); } return(pObjectToInitialize); }
/// <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> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The modified parent element</returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { try { // Store the type. pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); // Store all items. IDictionary lDictionary = pObject as IDictionary; if (lDictionary != null) { foreach (var lKey in lDictionary.Keys) { XElement lEntryElement = new XElement(XConstants.ITEM_TAG); IXSerializationContract lKeyContract = pSerializationContext.SelectContract(lEntryElement, lKey); if (lKeyContract != null) { XElement lKeyElement = new XElement(XConstants.KEY_TAG); lKeyContract.Write(lKey, lKeyElement, pSerializationContext); lEntryElement.Add(lKeyElement); } object lValue = lDictionary[lKey]; IXSerializationContract lValueContract = pSerializationContext.SelectContract(lEntryElement, lValue); if (lValueContract != null) { // Add the type of the value. Type lValueType = lDictionary.GetType().GetGenericArguments()[1]; if (object.ReferenceEquals(lValue, null) == false) { lValueType = lValue.GetType(); } lEntryElement.Add(pSerializationContext.ReferenceType(lValueType)); XElement lValuelement = new XElement(XConstants.VALUE_TAG); lValueContract.Write(lValue, lValuelement, pSerializationContext); lEntryElement.Add(lValuelement); } pParentElement.Add(lEntryElement); } } } catch { // Hack to avoid serialization crash due to DynamicTypedObject in tactical data. Need to make a serializer but want to avoid other to stop cretaing scenario :-) } return(pParentElement); }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public override SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject is PropertyInfo) { PropertyInfo lPropertyInfo = pObject as PropertyInfo; if (lPropertyInfo.CanWrite == false) { object[] lAttributes = lPropertyInfo.GetCustomAttributes(typeof(XInternalSerializationAttribute), true); if (!lAttributes.Any()) { return new SupportPriority(SupportLevel.Type, 0); } } } return SupportPriority.CANNOT_SUPPORT; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pObject">The object to test.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public override SupportPriority CanManage(object pObject, IXSerializationContext pSerializationContext) { if (pObject is PropertyInfo) { PropertyInfo lPropertyInfo = pObject as PropertyInfo; object[] lAttributes = lPropertyInfo.GetCustomAttributes(typeof (XInternalSerializationAttribute), true); if (lAttributes.Any()) { ForceWriteXSerializationAttribute lForceWriteAttribute = lAttributes.OfType<ForceWriteXSerializationAttribute>().FirstOrDefault(); if (lPropertyInfo.CanRead && lForceWriteAttribute != null) { return new SupportPriority(SupportLevel.Type, 0); } } } return SupportPriority.CANNOT_SUPPORT; }
/// <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 virtual object Read(object pObjectToInitialize, XElement pParentElement, IXSerializationContext pSerializationContext) { XAttribute lAttribute = pParentElement.Attribute(XConstants.REFERENCE_ATTRIBUTE); try { int lReference = Convert.ToInt32(lAttribute.Value.Trim(), CultureInfo.InvariantCulture); return pSerializationContext.GetObjectByReference(lReference); } catch (FormatException) { IXmlLineInfo lInfo = pParentElement; pSerializationContext.PushError(new XSerializationError(XErrorType.Parsing, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } catch (OverflowException) { IXmlLineInfo lInfo = pParentElement; pSerializationContext.PushError(new XSerializationError(XErrorType.NumberOverflow, lInfo.LineNumber, lInfo.LinePosition, pSerializationContext.CurrentFile, string.Empty)); } return null; }
/// <summary> /// This method determines whether this instance can manage the specified object. /// </summary> /// <param name="pParentElement">The element to manage.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The depth of inheritance or -1 if the contract cannot support.</returns> public override SupportPriority CanManage(XElement pParentElement, IXSerializationContext pSerializationContext) { bool lVersionFound; Version lXVersion = pSerializationContext.GetSerializationParameter<Version>("Version", out lVersionFound); if (lVersionFound == false) { // When no version is specified, only allowing the contract handling the last version. if (this.CanManageLastVersion == false) { return SupportPriority.CANNOT_SUPPORT; } } else if (lXVersion < this.MinVersion || lXVersion > this.MaxVersion) { // Version out of range. return SupportPriority.CANNOT_SUPPORT; } return base.CanManage(pParentElement, pSerializationContext); }
/// <summary> /// This method creates the specified element. /// </summary> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The created object.</returns> public virtual object Create(XElement pParentElement, IXSerializationContext pSerializationContext) { XAttribute lAttribute = this.mTargetElement.Attribute(XConstants.EXTERNAL_REFERENCE_ATTRIBUTE); string lExternalReference = lAttribute.Value; string lFullExternalReference; bool lIsRelative; if (Path.IsPathRooted(lExternalReference)) { lFullExternalReference = lExternalReference; lIsRelative = false; } else { lFullExternalReference = pSerializationContext.CurrentDirectory.FullName + Path.DirectorySeparatorChar + lExternalReference; lIsRelative = true; } XSerializer lDeserializer = new XSerializer(pSerializationContext.ExternalReferenceResolver); object lResult = lDeserializer.Deserialize(lFullExternalReference); pSerializationContext.ExternalReferenceResolver.RegisterExternalReference(lResult, lFullExternalReference, lIsRelative); return lResult; }
/// <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> /// <exception cref="System.NotImplementedException"></exception> public object Create(XElement pParentElement, IXSerializationContext pSerializationContext) { return null; }
/// <summary> /// This method checks if the object can be managed by the contract. /// </summary> /// <param name="pObjectElement">The element to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns>A support priority</returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(XElement pObjectElement, IXSerializationContext pSerializationContext) { return SupportPriority.CANNOT_SUPPORT; }
/// <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 XElement. /// </returns> public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { if (pObject != null) { Type lNullableType = typeof(Nullable<>).MakeGenericType(pObject.GetType()); pParentElement.Add(pSerializationContext.ReferenceType(lNullableType)); } XElement lValueElement = new XElement(XConstants.VALUE_TAG); IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(pParentElement, pObject); if (lSerializationContract != null) { lSerializationContract.Write(pObject, lValueElement, pSerializationContext); } pParentElement.Add(lValueElement); return pParentElement; }
/// <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; }
/// <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 checks if the object type can be managed by the contract. /// </summary> /// <param name="pObjectType">The object type to manage.</param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns> /// The support priority or SupportPriority.CANNOT_SUPPORT /// </returns> /// <remarks> /// The object can be a type, a property info, ... /// </remarks> public SupportPriority CanManage(Type pObjectType, IXSerializationContext pSerializationContext) { return SupportPriority.CANNOT_SUPPORT; }
/// <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 virtual object Read(object pObjectToInitialize, XElement pElement, IXSerializationContext pSerializationContext) { return pObjectToInitialize; }
/// <summary> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns>The modified parent element</returns> public virtual XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); XElement lValuelement = new XElement(XConstants.VALUE_TAG); lValuelement.Value = pObject.ToString(); pParentElement.Add(lValuelement); return pParentElement; }
/// <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; }