public static void XmlWriterSerialize(XmlWriter writer, object source, Type refType) { if (writer != null) { Type objType = (source == null) ? Generic <object> .Type : source.GetType(); if (((source == null) && (refType != null)) && KnownTypes.ContainsValue(refType)) { objType = refType; } if ((source == null) || IsSimpleType(objType)) { XmlSerializer serializer = new XmlSerializer(objType); serializer.Serialize(writer, source); } else if (source is Type) { writer.WriteStartElement("Type"); writer.WriteString(GetTypeName(source as Type)); writer.WriteEndElement(); } else if (source is Color) { Color color = (Color)source; writer.WriteStartElement("Color"); if (color.IsNamedColor) { writer.WriteAttributeString("IsNamedColor", bool.TrueString); writer.WriteString(color.Name); } else { writer.WriteString(color.ToArgb().ToString("x")); } writer.WriteEndElement(); } else if ((source is NameObjectCollectionBase) && HasEmptyConstructor(objType)) { XmlSerializeNameObjects(writer, source as NameObjectCollectionBase, null); } else if ((source is IDictionary) && HasEmptyConstructor(objType)) { XmlSerializeIDictionary(writer, source as IDictionary, null, null); } else if ((source is IList) && (objType.IsArray || HasEmptyConstructor(objType))) { XmlSerializeIList(writer, source as IList, null); } else { try { new XmlSerializer(objType).Serialize(writer, source); } catch (Exception ex) { throw new NotSupportedException(string.Format("The {0} is not supported by XmlSerialize.", objType.FullName), ex); } } } }
private static bool IsKnownType(Type type) { if (type.IsArray) { return(KnownTypes.ContainsValue(TypeHelper.GetArrayItemType(type))); } if (type.IsGenericType) { return(false); } return(KnownTypes.ContainsValue(type)); }