private void WriteObject(string name, object value, XContainer parent, IDictionary <string, XNamespace> namespaces) { if (HaveCustomValueConvertor(value.GetType())) { var valueConvertorType = reflection.GetGenericTypeOf(typeof(IValueConvertor <>), value); var convertor = serviceLocator.Resolve(valueConvertorType); var elementName = GetXmlNamespace(namespaces, value.GetType()) + name; var convertedValue = reflection.InvokeToElement(convertor, value, v => GetXmlNamespace(namespaces, v)); convertedValue = ApplyMessageSerializationBehaviorIfNecessary(value.GetType(), convertedValue); parent.Add(new XElement(elementName, convertedValue)); } else if (HaveCustomSerializer(value.GetType())) { var customSerializer = customElementSerializers.First(s => s.CanSerialize(value.GetType())); var elementName = GetXmlNamespace(namespaces, value.GetType()) + name; var element = customSerializer.ToElement(value, v => GetXmlNamespace(namespaces, v)); var customElement = new XElement(elementName, element); customElement = ApplyMessageSerializationBehaviorIfNecessary(value.GetType(), customElement); parent.Add(customElement); } else if (ShouldPutAsString(value)) { var elementName = GetXmlNamespace(namespaces, value.GetType()) + name; parent.Add(new XElement(elementName, FormatAsString(value))); } else if (value is byte[]) { var elementName = GetXmlNamespace(namespaces, typeof(byte[])) + name; parent.Add(new XElement(elementName, Convert.ToBase64String((byte[])value))); } else if (ShouldTreatAsDictionary(value.GetType())) { XElement list = GetContentWithNamespace(value, namespaces, name); parent.Add(list); var itemCount = 0; foreach (var item in ((IEnumerable)value)) { if (item == null) { continue; } itemCount += 1; if (itemCount > MaxNumberOfAllowedItemsInCollection) { throw new UnboundedResultSetException("You cannot send collections with more than 256 items (" + value + " " + name + ")"); } var entry = new XElement("entry"); var keyProp = reflection.Get(item, "Key"); if (keyProp == null) { continue; } WriteObject("Key", keyProp, entry, namespaces); var propVal = reflection.Get(item, "Value"); if (propVal != null) { WriteObject("Value", propVal, entry, namespaces); } list.Add(entry); } } else if (value is IEnumerable) { XElement list = GetContentWithNamespace(value, namespaces, name); parent.Add(list); var itemCount = 0; foreach (var item in ((IEnumerable)value)) { if (item == null) { continue; } itemCount += 1; if (itemCount > MaxNumberOfAllowedItemsInCollection) { throw new UnboundedResultSetException("You cannot send collections with more than 256 items (" + value + " " + name + ")"); } WriteObject("value", item, list, namespaces); } } else { XElement content = GetContentWithNamespace(value, namespaces, name); foreach (var property in reflection.GetProperties(value)) { var propVal = reflection.Get(value, property); if (propVal == null) { continue; } WriteObject(property, propVal, content, namespaces); } content = ApplyMessageSerializationBehaviorIfNecessary(value.GetType(), content); parent.Add(content); } }