/// <summary> /// Writes the specified p object. /// </summary> /// <param name="pObject">The p object.</param> /// <param name="pParentElement">The p parent element.</param> /// <param name="pSerializationContext">The p serialization context.</param> /// <returns></returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { PropertyInfo lPropertyInfo = pObject as PropertyInfo; XElement lObjectElement = new XElement(lPropertyInfo.Name); ParameterInfo[] lParameters = lPropertyInfo.GetIndexParameters(); if (lParameters.Length == 0) { object lPropertyValue = lPropertyInfo.GetValue(pSerializationContext.CurrentObject, null); IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(lObjectElement, lPropertyInfo.PropertyType); if (lPropertyValue == null) { lSerializationContract = pSerializationContext.SelectContract(lObjectElement, null); } else { lSerializationContract = pSerializationContext.SelectContract(lObjectElement, lPropertyValue); } if (lSerializationContract != null) { lSerializationContract.Write(lPropertyValue, lObjectElement, pSerializationContext); } pParentElement.Add(lObjectElement); } return(pParentElement); }
/// <summary> /// This method serializes an object into an XElement. /// </summary> /// <param name="pObjectToSerialize">The object to serialize.</param> /// <returns>A valid XElement if the serialization succeed, null otherwise.</returns> public virtual XElement Serialize(object pObjectToSerialize) { this.IsWriting = true; this.mErrors.Clear(); XElement lParentElement = new XElement(XConstants.ROOT_TAG); IXSerializationContract lContract = this.SelectContract(null, null, null, pObjectToSerialize); if (lContract != null) { if (this.mCurrentDirectory == null) { this.mCurrentDirectory = new DirectoryInfo(Environment.CurrentDirectory); } // Write the object. lContract.Write(pObjectToSerialize, lParentElement, this); XElement lTypeContainerElement = new XElement(XConstants.TYPE_CONTAINER_TAG); foreach (KeyValuePair <Type, XElement> lTypeInfo in this.mElementsByType) { lTypeInfo.Value.SetAttributeValue(XConstants.TYPE_REF_ATTRIBUTE, this.mTypeReferencesByType[lTypeInfo.Key]); lTypeContainerElement.Add(lTypeInfo.Value); } lParentElement.AddFirst(lTypeContainerElement); return(lParentElement); } 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 serializes the object in to an XElement. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement"></param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns> /// The modified XElement. /// </returns> public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); for (int lGenericIndex = 0; lGenericIndex < pObject.GetType().GetGenericArguments().Count(); lGenericIndex++) { string lItemName = XConstants.TUPLE_ITEM_TAG + (lGenericIndex + 1); IXSerializationContract lKeyContract = pSerializationContext.SelectContract(null, null, pObject.GetPropertyType(lItemName), null); if (lKeyContract != null) { XElement lKeyElement = new XElement(lItemName); lKeyContract.Write(pObject.GetPropertyValue(lItemName), lKeyElement, pSerializationContext); pParentElement.Add(lKeyElement); } } return(pParentElement); }
/// <summary> /// This method writes the specified object. /// </summary> /// <param name="pObject">The object to serialize (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 modified parent element</returns> public virtual XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { Type lFieldType; object lFieldObject = pSerializationContext.CurrentObject.GetFieldValue(this.TypedAttribute.FieldName, out lFieldType); XElement lFieldElement = new XElement("Field"); lFieldElement.SetAttributeValue("fieldName", this.TypedAttribute.FieldName); IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(null, lFieldObject); if (lSerializationContract != null) { lSerializationContract.Write(lFieldObject, lFieldElement, pSerializationContext); } pParentElement.Add(lFieldElement); this.Attribute = null; 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 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 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>lList.GetType().GetGenericArguments()[0] /// <returns>The modified parent element</returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { // Store the type. pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); // Store all items. IList lList = pObject as IList; foreach (object lItem in lList) { XElement lItemElement = new XElement(XConstants.ITEM_TAG); IXSerializationContract lContract = pSerializationContext.SelectContract(lItemElement, lItem); if (lContract != null) { lContract.Write(lItem, lItemElement, pSerializationContext); } pParentElement.Add(lItemElement); } return(pParentElement); }
/// <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 System.Xml.Linq.XElement Write(object pObject, System.Xml.Linq.XElement pParentElement, IXSerializationContext pSerializationContext) { IXTemplate lTemplate = pObject as IXTemplate; if (lTemplate != null) { // Creating the templated object. object lTemplatedObject = lTemplate.Create(); if (lTemplatedObject != null) { IXSerializationContract lContract = pSerializationContext.SelectContract(null, lTemplatedObject); if (lContract != null) { lContract.Write(lTemplatedObject, pParentElement, pSerializationContext); } } } return(pParentElement); }
/// <summary> /// Writes the specified object. /// </summary> /// <param name="pObject">The object.</param> /// <param name="pParentElement">The parent element.</param> /// <param name="pSerializationContext">The serialization context.</param> /// <returns></returns> public override XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); // Get public propertyinfo and the private one bearing a XSerializationAttribute attribute. PropertyInfo[] lPublicPropertyInfos = pObject.GetType().GetProperties(); PropertyInfo[] lNonPublicPropertyInfos = pObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic); PropertyInfo[] lFilteredPropertyInfos = lPublicPropertyInfos.Union(lNonPublicPropertyInfos.Where(pElt => pElt.GetCustomAttributes(typeof(IXSerializationAttribute), true).Any())).ToArray(); PropertyInfo[] lSortedFilteredPropertyInfos = lFilteredPropertyInfos.Select(pX => new { Property = pX, Attribute = (OrderXSerializationAttribute)Attribute.GetCustomAttribute(pX, typeof(OrderXSerializationAttribute), true) }).OrderBy(pX => pX.Attribute != null ? pX.Attribute.Order : Int32.MaxValue).ThenBy(pX => pX.Property.Name).Select(pX => pX.Property).ToArray(); pParentElement.SetAttributeValue(XConstants.ID_ATTRIBUTE, pSerializationContext.GetObjectReference(pObject)); foreach (PropertyInfo lPropertyInfo in lSortedFilteredPropertyInfos) { IXSerializationContract lSerializationContract = pSerializationContext.SelectContract(null, lPropertyInfo); if (lSerializationContract != null) { lSerializationContract.Write(lPropertyInfo, pParentElement, pSerializationContext); } } return(pParentElement); }
/// <summary> /// THis method serializes the object in to an XElement. /// </summary> /// <param name="pObject">The object to serialize.</param> /// <param name="pParentElement"></param> /// <param name="pSerializationContext">The context of serialization</param> /// <returns> /// The modified XElement. /// </returns> public XElement Write(object pObject, XElement pParentElement, IXSerializationContext pSerializationContext) { pParentElement.Add(pSerializationContext.ReferenceType(pObject.GetType())); IXSerializationContract lKeyContract = pSerializationContext.SelectContract(null, null, pObject.GetPropertyType("Key"), null); if (lKeyContract != null) { XElement lKeyElement = new XElement(XConstants.KEY_TAG); lKeyContract.Write(pObject.GetPropertyValue("Key"), lKeyElement, pSerializationContext); pParentElement.Add(lKeyElement); } IXSerializationContract lValueContract = pSerializationContext.SelectContract(null, null, pObject.GetPropertyType("Value"), null); if (lValueContract != null) { XElement lValueElement = new XElement(XConstants.VALUE_TAG); lValueContract.Write(pObject.GetPropertyValue("Value"), lValueElement, pSerializationContext); pParentElement.Add(lValueElement); } return(pParentElement); }