Exemple #1
0
        private static void XmlSerializeIList(XmlWriter writer, IList obj, Type itemType)
        {
            Type objType = obj.GetType();

            writer.WriteStartElement(GetCleanName(objType.Name));
            IEnumerator etor = obj.GetEnumerator();

            if (itemType == null)
            {
                if (objType.IsArray)
                {
                    itemType = TypeHelper.GetArrayItemType(objType);
                }
                else if (objType.IsGenericType)
                {
                    string itemTypeName = Arithmetic.GetInlineItem(objType.AssemblyQualifiedName, 1, '[', ']');
                    if (!string.IsNullOrEmpty(itemTypeName))
                    {
                        itemType = Type.GetType(itemTypeName);
                    }
                }
            }
            bool writeItemType = (itemType == null) || itemType.Equals(Generic <object> .Type);

            if (!(writeItemType || SimpleTypes.ContainsValue(itemType)))
            {
                writer.WriteAttributeString("itemType", GetTypeName(itemType));
            }
            while (etor.MoveNext())
            {
                object item = etor.Current;
                if (item != null)
                {
                    Type type = item.GetType();
                    if (!((!writeItemType && (type == itemType)) || IsKnownType(type)))
                    {
                        XmlNode element = XmlSerialize(item, true);
                        writer.WriteNode(new XmlNodeReader(element), true);
                    }
                    else
                    {
                        XmlWriterSerialize(writer, item, itemType);
                    }
                }
                else if (itemType == null)
                {
                    XmlWriterSerialize(writer, null, Generic <object> .Type);
                }
                else
                {
                    XmlWriterSerialize(writer, null, itemType);
                }
            }
            writer.WriteEndElement();
        }
Exemple #2
0
 private static bool IsSimpleType(Type type)
 {
     if (type.IsArray)
     {
         return(SimpleTypes.ContainsValue(TypeHelper.GetArrayItemType(type)));
     }
     if (type.IsGenericType)
     {
         return(false);
     }
     return(SimpleTypes.ContainsValue(type));
 }
Exemple #3
0
        private static void XmlSerializeIDictionary(XmlWriter writer, IDictionary obj, Type keyType, Type valueType)
        {
            Type objType = obj.GetType();

            writer.WriteStartElement(GetCleanName(objType.Name));
            IDictionaryEnumerator de = obj.GetEnumerator();

            if (objType.IsGenericType)
            {
                if (keyType == null)
                {
                    string keyTypeName = Arithmetic.GetInlineItem(objType.FullName, 1, '[', ']');
                    if (!string.IsNullOrEmpty(keyTypeName))
                    {
                        keyType = Type.GetType(keyTypeName);
                    }
                }
                if (valueType == null)
                {
                    string valueTypeName = Arithmetic.GetInlineItem(objType.FullName, 2, '[', ']');
                    if (!string.IsNullOrEmpty(valueTypeName))
                    {
                        valueType = Type.GetType(valueTypeName);
                    }
                }
            }
            bool writeKeyType = (keyType == null) || keyType.Equals(Generic <object> .Type);

            if (!(writeKeyType || SimpleTypes.ContainsValue(keyType)))
            {
                writer.WriteAttributeString("keyType", GetTypeName(keyType));
            }
            bool writeValueType = (valueType == null) || valueType.Equals(Generic <object> .Type);

            if (!(writeValueType || SimpleTypes.ContainsValue(valueType)))
            {
                writer.WriteAttributeString("valueType", GetTypeName(valueType));
            }
            while (de.MoveNext())
            {
                Type type;
                writer.WriteStartElement("Item");
                if (de.Key != null)
                {
                    type = de.Key.GetType();
                    if (!((!writeKeyType && (type == keyType)) || IsKnownType(type)))
                    {
                        writer.WriteAttributeString("keyType", GetTypeName(type));
                    }
                }
                if (de.Value != null)
                {
                    type = de.Value.GetType();
                    if (!((!writeValueType && (type == valueType)) || IsKnownType(type)))
                    {
                        writer.WriteAttributeString("valueType", GetTypeName(type));
                    }
                }
                XmlWriterSerialize(writer, de.Key, keyType);
                XmlWriterSerialize(writer, de.Value, valueType);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }