private static void GetCollectionElementTypes(Type type, ArrayMapping mapping)
        {
            PropertyInfo propertyInfo = null;
            Type         type2        = type;

            while (type2 != null)
            {
                MemberInfo[] defaultMembers = type.GetDefaultMembers();
                if (defaultMembers != null)
                {
                    for (int i = 0; i < defaultMembers.Length; i++)
                    {
                        if (defaultMembers[i] is PropertyInfo)
                        {
                            PropertyInfo propertyInfo2 = (PropertyInfo)defaultMembers[i];
                            if (propertyInfo2.CanRead && propertyInfo2.GetGetMethod().GetParameters().Length == 1 && (propertyInfo == null || propertyInfo.PropertyType == typeof(object)))
                            {
                                propertyInfo = propertyInfo2;
                            }
                        }
                    }
                }
                if (propertyInfo != null)
                {
                    break;
                }
                type2 = type2.BaseType;
            }
            if (propertyInfo == null)
            {
                throw new Exception("NoDefaultAccessors");
            }
            if (type.GetMethod("Add", new Type[1]
            {
                propertyInfo.PropertyType
            }) == null)
            {
                throw new Exception("NoAddMethod");
            }
            mapping.ItemType = propertyInfo.PropertyType;
            IList customAttributes = propertyInfo.PropertyType.GetCustomAttributes(typeof(XmlElementClassAttribute), inherit: true);

            if (customAttributes != null && customAttributes.Count > 0)
            {
                foreach (XmlElementClassAttribute item in customAttributes)
                {
                    mapping.ElementTypes.Add((item.ElementName != string.Empty) ? item.ElementName : item.Type.Name, item.Type);
                }
            }
            else
            {
                string name = propertyInfo.PropertyType.Name;
                mapping.ElementTypes.Add(name, propertyInfo.PropertyType);
            }
        }
Beispiel #2
0
        private void WriteArrayContent(XmlWriter writer, object array, ArrayMapping mapping, MemberMapping member, int nestingLevel, string ns)
        {
            Dictionary <string, Type> elementTypes = mapping.ElementTypes;

            foreach (object item in (IEnumerable)array)
            {
                string text = null;
                bool   flag = false;
                if (member != null && member.XmlAttributes.XmlArrayItems.Count > nestingLevel)
                {
                    XmlArrayItemAttribute xmlArrayItemAttribute = member.XmlAttributes.XmlArrayItems[nestingLevel];
                    text = xmlArrayItemAttribute.ElementName;
                    flag = xmlArrayItemAttribute.IsNullable;
                }
                else
                {
                    Type        serializationType = GetSerializationType(item);
                    TypeMapping typeMapping       = TypeMapper.GetTypeMapping(serializationType);
                    if (typeMapping != null)
                    {
                        text = typeMapping.Name;
                        ns   = typeMapping.Namespace;
                    }
                    else
                    {
                        foreach (KeyValuePair <string, Type> item2 in elementTypes)
                        {
                            if (item2.Value == serializationType)
                            {
                                text = item2.Key;
                                break;
                            }
                        }
                    }
                }
                if (text == null)
                {
                    throw new Exception("No array element name.");
                }
                if (item != null)
                {
                    WriteObject(writer, item, text, ns, member, nestingLevel + 1);
                }
                else if (flag)
                {
                    WriteNilElement(writer, text, ns);
                }
            }
        }
        private static ArrayMapping ImportArrayMapping(Type type)
        {
            ArrayMapping arrayMapping = new ArrayMapping(type);

            arrayMapping.ElementTypes = new Dictionary <string, Type>();
            if (type.IsArray)
            {
                Type type2 = arrayMapping.ItemType = type.GetElementType();
                arrayMapping.ElementTypes.Add(type2.Name, type2);
            }
            else
            {
                GetCollectionElementTypes(type, arrayMapping);
            }
            return(arrayMapping);
        }
Beispiel #4
0
 private void WriteArray(XmlWriter writer, object component, object array, string name, string ns, MemberMapping member, ArrayMapping mapping, int nestingLevel)
 {
     if (ShouldSerializeArray(array))
     {
         WriteStartElement(writer, component, name, ns, member);
         WriteArrayContent(writer, array, mapping, member, nestingLevel, ns);
         writer.WriteEndElement();
     }
 }
        private object ReadArrayContent(object array, ArrayMapping mapping, MemberMapping member, int nestingLevel)
        {
            IList list = (IList)array;

            if (m_reader.IsEmptyElement)
            {
                m_reader.Skip();
            }
            else
            {
                m_reader.ReadStartElement();
                m_reader.MoveToContent();
                while (m_reader.NodeType != XmlNodeType.EndElement && m_reader.NodeType != 0)
                {
                    if (m_reader.NodeType == XmlNodeType.Element)
                    {
                        string localName = m_reader.LocalName;
                        _ = m_reader.NamespaceURI;
                        Type value = null;
                        bool flag  = false;
                        if (member != null && member.XmlAttributes.XmlArrayItems.Count > nestingLevel)
                        {
                            if (localName == member.XmlAttributes.XmlArrayItems[nestingLevel].ElementName)
                            {
                                XmlArrayItemAttribute xmlArrayItemAttribute = member.XmlAttributes.XmlArrayItems[nestingLevel];
                                value = xmlArrayItemAttribute.Type;
                                flag  = xmlArrayItemAttribute.IsNullable;
                            }
                        }
                        else
                        {
                            XmlElementAttributes xmlElementAttributes = null;
                            if (base.XmlOverrides != null)
                            {
                                XmlAttributes xmlAttributes = base.XmlOverrides[mapping.ItemType];
                                if (xmlAttributes != null && xmlAttributes.XmlElements != null)
                                {
                                    xmlElementAttributes = xmlAttributes.XmlElements;
                                }
                            }
                            if (xmlElementAttributes == null)
                            {
                                mapping.ElementTypes.TryGetValue(localName, out value);
                            }
                            else
                            {
                                foreach (XmlElementAttribute item in xmlElementAttributes)
                                {
                                    if (localName == item.ElementName)
                                    {
                                        value = item.Type;
                                        break;
                                    }
                                }
                            }
                        }
                        if (value != null)
                        {
                            object value2;
                            if (flag && m_reader.GetAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance") == "true")
                            {
                                m_reader.Skip();
                                value2 = null;
                            }
                            else
                            {
                                value2 = ReadObject(value, member, nestingLevel + 1);
                            }
                            list.Add(value2);
                        }
                        else
                        {
                            m_reader.Skip();
                        }
                    }
                    else
                    {
                        m_reader.Skip();
                    }
                    m_reader.MoveToContent();
                }
                m_reader.ReadEndElement();
            }
            return(array);
        }