public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType) { Type keyType = null; Type valueType = null; if ((expectedType != null) && (expectedType.IsGenericType)) { keyType = GenericArgumentLookup(expectedType, 0); valueType = GenericArgumentLookup(expectedType, 1); xmlElement.SetAttribute("keyType", keyType.AssemblyQualifiedNameWithoutVersion()); xmlElement.SetAttribute("valueType", valueType.AssemblyQualifiedNameWithoutVersion()); } // Make sure the element gets created when there are zero elements in the list xmlElement.MakeSureElementExists(); IDictionary dict = o as System.Collections.IDictionary; foreach (DictionaryEntry entry in dict) { Xml.XmlDocElement itemElement = xmlElement.AddElement("Item"); Xml.XmlDocElement keyElement = itemElement.AddElement("Key"); DefaultSerializer.WriteObject(entry.Key, keyElement, keyType); Xml.XmlDocElement valueElement = itemElement.AddElement("Value"); DefaultSerializer.WriteObject(entry.Value, valueElement, valueType); } }
public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType) { Type itemType = null; if ((expectedType != null) && (expectedType.IsGenericType)) { itemType = GenericArgumentLookup(expectedType, 0); } // Make sure the element gets created when there are zero elements in the list xmlElement.MakeSureElementExists(); foreach (object childValue in (o as System.Collections.IEnumerable)) { Xml.XmlDocElement childElement = xmlElement.AddElement("Item"); DefaultSerializer.WriteObject(childValue, childElement, itemType); } }
public override void WriteObject(object o, Utils.Xml.XmlDocElement xmlElement, Type expectedType) { if (o == null) { xmlElement.SetAttribute("null", "true"); return; } Type type = o.GetType(); string typeName = null; string canRecreateTypeName; if (!_canRecreateLookup.TryGetValue(type, out canRecreateTypeName)) { canRecreateTypeName = typeName; if (canRecreateTypeName == null) { canRecreateTypeName = type.AssemblyQualifiedNameWithoutVersion(); } bool canRecreate = (Type.GetType(canRecreateTypeName) != null); if (!canRecreate) { canRecreateTypeName = type.AssemblyQualifiedName; canRecreate = (Type.GetType(canRecreateTypeName) != null); if (!canRecreate) { canRecreateTypeName = ""; } } _canRecreateLookup.Add(type, canRecreateTypeName); } if (canRecreateTypeName == "") { throw new Exception("Could not recreate type from Assembly Qualified Name"); } if (expectedType == null) { xmlElement.SetAttribute("type", canRecreateTypeName); } if (o is DateTime) { xmlElement.Value = ((DateTime)o).ToString("yyyy-MM-dd HH:mm:ss"); return; } if (o is IConvertible) { xmlElement.Value = Convert.ChangeType(o, typeof(string)) as string; return; } if (type.IsPrimitive) { throw new Exception("Primitive that's not convertible"); } if (type.IsArray) { Type itemType = type.GetElementType(); Array array = o as Array; foreach (object childValue in array) { Xml.XmlDocElement childElement = xmlElement.AddElement("Item"); WriteObject(childValue, childElement, itemType); } return; } for (; type != typeof(object); type = type.BaseType) { foreach (BaseTypeSerializer typeSerializer in _typeSerializers) { if (typeSerializer.CanSerializeType(type)) { typeSerializer.DefaultSerializer = this; typeSerializer.WriteObject(o, xmlElement, type); return; } } FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (FieldInfo field in fields) { object[] xmlIgnoreAttributes; string attributesKey = "ig:Field " + field.ToString(); if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes)) { xmlIgnoreAttributes = field.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), true); _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes); } if (xmlIgnoreAttributes.Length > 0) { continue; } attributesKey = "igEmpty:Field " + field.ToString(); if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes)) { xmlIgnoreAttributes = field.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreIfNullOrEmptyAttribute), true); _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes); } bool ignoreIfNullOrEmpty = (xmlIgnoreAttributes.Length > 0); object childValue = field.GetValue(o); if (ignoreIfNullOrEmpty) { if (childValue == null) { continue; } if ((childValue is string) && string.IsNullOrEmpty(childValue as string)) { continue; } } Xml.XmlDocElement childElement = xmlElement[field.Name]; if (childValue == o) { childElement.SetAttribute("selfReference", "true"); } else { WriteObject(childValue, childElement, field.FieldType); } } PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (PropertyInfo property in properties) { if (!property.CanRead || !property.CanWrite) { continue; } object[] xmlIgnoreAttributes; string attributesKey = "ig:Property " + property.ToString(); if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes)) { xmlIgnoreAttributes = property.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), true); _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes); } if (xmlIgnoreAttributes.Length > 0) { continue; } attributesKey = "igEmpty:Property " + property.ToString(); if (!_customAttributesLookup.TryGetValue(attributesKey, out xmlIgnoreAttributes)) { xmlIgnoreAttributes = property.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreIfNullOrEmptyAttribute), true); _customAttributesLookup.Add(attributesKey, xmlIgnoreAttributes); } bool ignoreIfNullOrEmpty = (xmlIgnoreAttributes.Length > 0); object childValue = property.GetValue(o, null); if (ignoreIfNullOrEmpty) { if (childValue == null) { continue; } if ((childValue is string) && string.IsNullOrEmpty(childValue as string)) { continue; } } Xml.XmlDocElement childElement = xmlElement[property.Name]; if (childValue == o) { childElement.SetAttribute("selfReference", "true"); } else { WriteObject(childValue, childElement, property.PropertyType); } } } }