예제 #1
0
        private void WriteObjectContent(XmlWriter writer, object component, object obj, string name, string ns, MemberMapping member, int nestingLevel)
        {
            TypeMapping typeMapping = TypeMapper.GetTypeMapping(GetSerializationType(obj));

            if (name == null)
            {
                name = typeMapping.Name;
                ns   = typeMapping.Namespace;
            }
            if (typeMapping is PrimitiveMapping)
            {
                WritePrimitive(writer, component, obj, name, ns, member, typeMapping);
            }
            else if (typeMapping is ArrayMapping)
            {
                WriteArray(writer, component, obj, name, ns, member, (ArrayMapping)typeMapping, nestingLevel);
            }
            else if (typeMapping is SpecialMapping)
            {
                WriteSpecialMapping(writer, component, obj, name, ns, member);
            }
            else if (typeMapping is StructMapping)
            {
                WriteStructure(writer, component, obj, name, ns, member, (StructMapping)typeMapping);
            }
        }
예제 #2
0
        private object ReadObjectContent(object value, MemberMapping member, int nestingLevel)
        {
            TypeMapping typeMapping = TypeMapper.GetTypeMapping(value.GetType());

            if (typeMapping is ArrayMapping)
            {
                ReadArrayContent(value, (ArrayMapping)typeMapping, member, nestingLevel);
            }
            else if (typeMapping is StructMapping)
            {
                ReadStructContent(value, (StructMapping)typeMapping);
            }
            else if (typeMapping is SpecialMapping)
            {
                ReadSpecialContent(value);
            }
            else
            {
                m_reader.Skip();
            }
            if (base.Host != null)
            {
                base.Host.OnDeserialization(value);
            }
            return(value);
        }
예제 #3
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);
                }
            }
        }
예제 #4
0
 private object ReadRoot(Type type)
 {
     try
     {
         m_reader.MoveToContent();
         TypeMapping typeMapping = TypeMapper.GetTypeMapping(type);
         if (m_reader.NamespaceURI != typeMapping.Namespace)
         {
             throw new XmlException(SRErrors.NoRootElement);
         }
         return(ReadObject(type, null, 0));
     }
     catch (XmlException)
     {
         throw;
     }
     catch (Exception innerException)
     {
         if (innerException is TargetInvocationException && innerException.InnerException != null)
         {
             innerException = innerException.InnerException;
         }
         string message;
         if (innerException is TargetInvocationException)
         {
             MethodBase targetSite = ((TargetInvocationException)innerException).TargetSite;
             message = SRErrors.DeserializationFailedMethod((targetSite != null) ? (targetSite.DeclaringType.Name + "." + targetSite.Name) : null);
         }
         else
         {
             message = SRErrors.DeserializationFailed(innerException.Message);
         }
         IXmlLineInfo xmlLineInfo = m_reader as IXmlLineInfo;
         XmlException ex2         = (xmlLineInfo == null) ? new XmlException(message, innerException) : new XmlException(message, innerException, xmlLineInfo.LineNumber, xmlLineInfo.LinePosition);
         throw ex2;
     }
 }