/// <summary> /// Serializes the properties an Object and appends them to the specified XmlNode. /// </summary> /// <param name="obj"></param> /// <param name="parent"></param> protected void SerializeProperties(object obj, XmlNode parent) { if (TypeInfo.IsCollection(obj.GetType())) { SetCollectionItems(obj, (ICollection)obj, parent); } else { XmlElement node = parent.OwnerDocument.CreateElement(_taglib.PROPERTIES_TAG); SetProperties(obj, node); parent.AppendChild(node); } }
/// <summary> /// Reads the properties of the specified node and sets them an the parent object. /// </summary> /// <param name="parent"></param> /// <param name="node"></param> /// <remarks> /// This is the central method which is called recursivly! /// </remarks> protected object GetProperties(object parent, XmlNode node) { if (parent == null) { return(parent); } // Get the properties XmlNodeList nl = node.SelectNodes(taglib.PROPERTIES_TAG + "/" + taglib.PROPERTY_TAG); // Properties found? if (nl == null || nl.Count == 0) { // No properties found... perhaps a collection? if (TypeInfo.IsCollection(parent.GetType())) { SetCollectionValues((ICollection)parent, node); } else { // Nothing to do here return(parent); } } // Loop the properties found foreach (XmlNode prop in nl) { // Collect the nodes type information about the property to deserialize ObjectInfo oi = GetObjectInfo(prop); // Enough info? if (oi.IsSufficient && !String.IsNullOrEmpty(oi.Name)) { object obj = null; // Create an instance, but note: arrays always need the size for instantiation if (TypeInfo.IsArray(oi.Type)) { int c = GetArrayLength(prop); obj = CreateArrayInstance(oi, c); } else { obj = CreateInstance(oi); } // Process the property's properties (recursive call of this method) if (obj != null) { obj = GetProperties(obj, prop); } // Setting the instance (or null) as the property's value PropertyInfo pi = parent.GetType().GetProperty(oi.Name); if (obj != null && pi != null) { pi.SetValue(parent, obj, null); } } } var identifyible = parent as IEntityClass; if (identifyible != null) { var list = GetIdentifyibleTypeList(identifyible.GetType()); if (!list.ContainsKey(identifyible.Id)) { list.Add(identifyible.Id, identifyible); return(list[identifyible.Id]); } return(list[identifyible.Id]); } return(parent); }
protected void SetCollectionItems(object obj, ICollection value, XmlNode parent) { // Validating the parameters if (obj == null || value == null || parent == null) { return; } try { ICollection coll = value; XmlElement collnode = parent.OwnerDocument.CreateElement(_taglib.ITEMS_TAG); parent.AppendChild(collnode); int cnt = 0; // What kind of Collection? if (TypeInfo.IsDictionary(coll.GetType())) { // IDictionary var dict = (IDictionary)coll; IDictionaryEnumerator de = dict.GetEnumerator(); while (de.MoveNext()) { XmlElement itemnode = parent.OwnerDocument.CreateElement(_taglib.ITEM_TAG); collnode.AppendChild(itemnode); object curr = de.Current; XmlElement propsnode = parent.OwnerDocument.CreateElement(_taglib.PROPERTIES_TAG); itemnode.AppendChild(propsnode); SetProperties(curr, propsnode); } } else { // Everything else IEnumerator ie = coll.GetEnumerator(); while (ie.MoveNext()) { object obj2 = ie.Current; XmlElement itemnode = parent.OwnerDocument.CreateElement(_taglib.ITEM_TAG); if (obj2 != null) { SetObjectInfoAttributes(null, obj2.GetType(), itemnode); } else { SetObjectInfoAttributes(null, null, itemnode); } itemnode.Attributes[_taglib.NAME_TAG].Value = "" + cnt; cnt++; collnode.AppendChild(itemnode); if (obj2 == null) { continue; } Type pt = obj2.GetType(); if (TypeInfo.IsCollection(pt)) { SetCollectionItems(obj, (ICollection)obj2, itemnode); } else { SetXmlElementFromBasicPropertyValue(itemnode, pt, obj2, parent); } // IsCollection? } // Loop collection } // IsDictionary? } catch (Exception exc) { if (!IgnoreSerialisationErrors) { throw exc; } else { // perhaps logging } } }
protected void SetProperty(object obj, object value, PropertyInfo pi, XmlNode parent) { // object val = value; ?? try { // Empty values are ignored (no need to restore null references or empty Strings) if (value == null || value.Equals("")) { return; } // Get the Type //Type pt = pi.PropertyType; Type pt = value.GetType(); // Check whether this property can be serialized and deserialized if (CheckPropertyHasToBeSerialized(pi) && (pt.IsSerializable || IgnoreSerializableAttribute) && (pi.CanWrite) && ((pt.IsPublic) || (pt.IsEnum))) { XmlElement prop = parent.OwnerDocument.CreateElement(_taglib.PROPERTY_TAG); SetObjectInfoAttributes(pi.Name, pt, prop); // Try to find a constructor for binary data. // If found remember the parameter's Type. Type binctortype = TypeInfo.GetBinaryConstructorType(pt); if (binctortype != null) // a binary contructor was found { /* * b. Trying to handle binary data */ SerializeBinaryObject(pi.GetValue(obj, null), binctortype, prop); } else if (TypeInfo.IsCollection(pt)) { /* * a. Collections ask for a specific handling */ SetCollectionItems(obj, (ICollection)value, prop); } else { /* * c. "normal" classes */ SetXmlElementFromBasicPropertyValue(prop, pt, value, parent); } // Append the property node to the paren XmlNode parent.AppendChild(prop); } } catch (Exception exc) { if (!IgnoreSerialisationErrors) { throw exc; } else { // perhaps logging } } }