SetContentModel() private method

private SetContentModel ( TextAccessor text, bool hasElements ) : void
text TextAccessor
hasElements bool
return void
        StructMapping ImportStructLikeMapping(StructModel model, string ns, bool openModel, XmlAttributes a) {
            if (model.TypeDesc.Kind == TypeKind.Root) return GetRootMapping();
            if (a == null)
                a = GetAttributes(model.Type, false);

            string typeNs = ns;
            if (a.XmlType != null && a.XmlType.Namespace != null)
                typeNs = a.XmlType.Namespace;
            else if (a.XmlRoot != null && a.XmlRoot.Namespace != null)
                typeNs = a.XmlRoot.Namespace;

            string typeName = XsdTypeName(model.Type, a, model.TypeDesc.Name);
            typeName = XmlConvert.EncodeLocalName(typeName);

            StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc, types, model.Type);
            if (mapping == null) {
                mapping = new StructMapping();
                mapping.TypeDesc = model.TypeDesc;
                mapping.Namespace = typeNs;
                mapping.TypeName = typeName;
                if (!mapping.IsAnonymousType)
                    types.Add(typeName, typeNs, mapping);
                else
                    anonymous[model.Type] = mapping;
                if (a.XmlType != null) {
                    mapping.IncludeInSchema = a.XmlType.IncludeInSchema;
                }

                if (model.TypeDesc.BaseTypeDesc != null) {
                    TypeModel baseModel = modelScope.GetTypeModel(model.Type.BaseType, false);
                    if (!(baseModel is StructModel)) {
                        //XmlUnsupportedInheritance=Using '{0}' as a base type for a class is not supported by XmlSerializer.
                         throw new NotSupportedException(Res.GetString(Res.XmlUnsupportedInheritance, model.Type.BaseType.FullName));
                    }
                    mapping.BaseMapping = ImportStructLikeMapping((StructModel)baseModel, mapping.Namespace, openModel, null);
                    ICollection values = mapping.BaseMapping.LocalAttributes.Values;
                    foreach (AttributeAccessor attribute in values) {
                        AddUniqueAccessor(mapping.LocalAttributes, attribute);
                    }
                    if (!mapping.BaseMapping.HasExplicitSequence()) {
                        values = mapping.BaseMapping.LocalElements.Values;
                        foreach (ElementAccessor e in values) {
                            AddUniqueAccessor(mapping.LocalElements, e);
                        }
                    }
                }
                ArrayList members = new ArrayList();
                TextAccessor textAccesor = null;
                bool hasElements = false;
                bool isSequence = false;
                
                foreach (MemberInfo memberInfo in model.GetMemberInfos()) {
                    if ((memberInfo.MemberType & (MemberTypes.Field | MemberTypes.Property)) == 0)
                        continue;
                    XmlAttributes memberAttrs = GetAttributes(memberInfo);
                    if (memberAttrs.XmlIgnore) continue;
                    FieldModel fieldModel = model.GetFieldModel(memberInfo);
                    if (fieldModel == null) continue;
                    try {
                        MemberMapping member = ImportFieldMapping(model, fieldModel, memberAttrs, mapping.Namespace);
                        if (member == null) continue;
                        if (mapping.BaseMapping != null) {
                            if (mapping.BaseMapping.Declares(member, mapping.TypeName)) continue;
                        }
                        isSequence |= member.IsSequence;
                        // add All memeber accessors to the scope accessors
                        AddUniqueAccessor(member, mapping.LocalElements, mapping.LocalAttributes, isSequence);

                        if (member.Text != null) {
                            if (!member.Text.Mapping.TypeDesc.CanBeTextValue && member.Text.Mapping.IsList)
                                throw new InvalidOperationException(Res.GetString(Res.XmlIllegalTypedTextAttribute, typeName, member.Text.Name, member.Text.Mapping.TypeDesc.FullName));
                            if (textAccesor != null) {
                                throw new InvalidOperationException(Res.GetString(Res.XmlIllegalMultipleText, model.Type.FullName));
                            }
                            textAccesor = member.Text;
                        }
                        if (member.Xmlns != null) {
                            if (mapping.XmlnsMember != null)
                                throw new InvalidOperationException(Res.GetString(Res.XmlMultipleXmlns, model.Type.FullName));
                            mapping.XmlnsMember = member;
                        }
                        if (member.Elements != null && member.Elements.Length != 0) {
                            hasElements = true;
                        }
                        members.Add(member);
                    }
                    catch (Exception e) {
                        if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                            throw;
                        }
                        throw CreateMemberReflectionException(fieldModel, e);
                    }
                    catch {
                        throw CreateMemberReflectionException(fieldModel, null);
                    }
                }
                mapping.SetContentModel(textAccesor, hasElements);
                if (isSequence) {
                    Hashtable ids = new Hashtable();
                    for (int i = 0; i < members.Count; i++) {
                        MemberMapping member = (MemberMapping)members[i];
                        if (!member.IsParticle)
                            continue;
                        if (member.IsSequence) {
                            if (ids[member.SequenceId] != null) {
                                throw new InvalidOperationException(Res.GetString(Res.XmlSequenceUnique, member.SequenceId.ToString(CultureInfo.InvariantCulture), "Order", member.Name));
                            }
                            ids[member.SequenceId] = member;
                        }
                        else {
                            throw new InvalidOperationException(Res.GetString(Res.XmlSequenceInconsistent, "Order", member.Name));
                        }
                    }
                    members.Sort(new MemberMappingComparer());
                }
                mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));

                if (mapping.BaseMapping == null) mapping.BaseMapping = GetRootMapping();

                if (mapping.XmlnsMember != null && mapping.BaseMapping.HasXmlnsMember)
                    throw new InvalidOperationException(Res.GetString(Res.XmlMultipleXmlns, model.Type.FullName));

                IncludeTypes(model.Type);
                typeScope.AddTypeMapping(mapping);
            }
            if (openModel)
                mapping.IsOpenModel = true;
            return mapping;
        }
        bool InitializeStructMembers(StructMapping mapping, StructModel model, bool openModel, string typeName, RecursionLimiter limiter) {
            if (mapping.IsFullyInitialized)
                return true;

                if (model.TypeDesc.BaseTypeDesc != null) {
                    TypeModel baseModel = modelScope.GetTypeModel(model.Type.BaseType, false);
                    if (!(baseModel is StructModel)) {
                        //XmlUnsupportedInheritance=Using '{0}' as a base type for a class is not supported by XmlSerializer.
                         throw new NotSupportedException(Res.GetString(Res.XmlUnsupportedInheritance, model.Type.BaseType.FullName));
                    }
                StructMapping baseMapping = ImportStructLikeMapping((StructModel)baseModel, mapping.Namespace, openModel, null, limiter);
                // check to see if the import of the baseMapping was deffered
                int baseIndex = limiter.DeferredWorkItems.IndexOf(baseMapping);
                if (baseIndex < 0) {
                    mapping.BaseMapping = baseMapping;

                    ICollection values = mapping.BaseMapping.LocalAttributes.Values;
                    foreach (AttributeAccessor attribute in values) {
                        AddUniqueAccessor(mapping.LocalAttributes, attribute);
                    }
                    if (!mapping.BaseMapping.HasExplicitSequence()) {
                        values = mapping.BaseMapping.LocalElements.Values;
                        foreach (ElementAccessor e in values) {
                            AddUniqueAccessor(mapping.LocalElements, e);
                        }
                    }
                }
                else {
                    // the import of the baseMapping was deffered, make sure that the derived mappings is deffered as well
                    if (!limiter.DeferredWorkItems.Contains(mapping)) {
                        limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                    }
                    // make sure that baseMapping get processed before the derived
                    int top = limiter.DeferredWorkItems.Count - 1;
                    if (baseIndex < top) {
                        ImportStructWorkItem baseMappingWorkItem = limiter.DeferredWorkItems[baseIndex];
                        limiter.DeferredWorkItems[baseIndex] = limiter.DeferredWorkItems[top];
                        limiter.DeferredWorkItems[top] = baseMappingWorkItem;
                    }
                    return false;
                }
            }
                ArrayList members = new ArrayList();
                TextAccessor textAccesor = null;
                bool hasElements = false;
                bool isSequence = false;
                
                foreach (MemberInfo memberInfo in model.GetMemberInfos()) {
                    if ((memberInfo.MemberType & (MemberTypes.Field | MemberTypes.Property)) == 0)
                        continue;
                    XmlAttributes memberAttrs = GetAttributes(memberInfo);
                    if (memberAttrs.XmlIgnore) continue;
                    FieldModel fieldModel = model.GetFieldModel(memberInfo);
                    if (fieldModel == null) continue;
                    try {
                    MemberMapping member = ImportFieldMapping(model, fieldModel, memberAttrs, mapping.Namespace, limiter);
                        if (member == null) continue;
                        if (mapping.BaseMapping != null) {
                            if (mapping.BaseMapping.Declares(member, mapping.TypeName)) continue;
                        }
                        isSequence |= member.IsSequence;
                        // add All memeber accessors to the scope accessors
                        AddUniqueAccessor(member, mapping.LocalElements, mapping.LocalAttributes, isSequence);

                        if (member.Text != null) {
                            if (!member.Text.Mapping.TypeDesc.CanBeTextValue && member.Text.Mapping.IsList)
                                throw new InvalidOperationException(Res.GetString(Res.XmlIllegalTypedTextAttribute, typeName, member.Text.Name, member.Text.Mapping.TypeDesc.FullName));
                            if (textAccesor != null) {
                                throw new InvalidOperationException(Res.GetString(Res.XmlIllegalMultipleText, model.Type.FullName));
                            }
                            textAccesor = member.Text;
                        }
                        if (member.Xmlns != null) {
                            if (mapping.XmlnsMember != null)
                                throw new InvalidOperationException(Res.GetString(Res.XmlMultipleXmlns, model.Type.FullName));
                            mapping.XmlnsMember = member;
                        }
                        if (member.Elements != null && member.Elements.Length != 0) {
                            hasElements = true;
                        }
                        members.Add(member);
                    }
                    catch (Exception e) {
                        if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                            throw;
                        }
                        throw CreateMemberReflectionException(fieldModel, e);
                    }
                }
                mapping.SetContentModel(textAccesor, hasElements);
                if (isSequence) {
                    Hashtable ids = new Hashtable();
                    for (int i = 0; i < members.Count; i++) {
                        MemberMapping member = (MemberMapping)members[i];
                        if (!member.IsParticle)
                            continue;
                        if (member.IsSequence) {
                            if (ids[member.SequenceId] != null) {
                                throw new InvalidOperationException(Res.GetString(Res.XmlSequenceUnique, member.SequenceId.ToString(CultureInfo.InvariantCulture), "Order", member.Name));
                            }
                            ids[member.SequenceId] = member;
                        }
                        else {
                            throw new InvalidOperationException(Res.GetString(Res.XmlSequenceInconsistent, "Order", member.Name));
                        }
                    }
                    members.Sort(new MemberMappingComparer());
                }
                mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));

                if (mapping.BaseMapping == null) mapping.BaseMapping = GetRootMapping();

                if (mapping.XmlnsMember != null && mapping.BaseMapping.HasXmlnsMember)
                    throw new InvalidOperationException(Res.GetString(Res.XmlMultipleXmlns, model.Type.FullName));

            IncludeTypes(model.Type, limiter);
                typeScope.AddTypeMapping(mapping);
            if (openModel)
                mapping.IsOpenModel = true;
            return true;
        }
        StructMapping ImportStructLikeMapping(StructModel model, string ns, XmlSchemaForm form) {
            if (model.TypeDesc.Kind == TypeKind.Root) return GetRootMapping();
            XmlAttributes a = GetAttributes(model.Type);

            string typeNs = ns;
            if (a.XmlType != null && a.XmlType.Namespace != null)
                typeNs = a.XmlType.Namespace;
            else if (a.XmlRoot != null && a.XmlRoot.Namespace != null)
                typeNs = a.XmlRoot.Namespace;

            string typeName = string.Empty;
            if (a.XmlType != null)
                typeName = a.XmlType.TypeName;
            if (typeName.Length == 0) 
                typeName = model.TypeDesc.Name;

            StructMapping mapping = (StructMapping)GetTypeMapping(typeName, typeNs, model.TypeDesc);
            if (mapping == null) {
                mapping = new StructMapping();
                mapping.TypeDesc = model.TypeDesc;
                mapping.Namespace = typeNs;
                mapping.TypeName = typeName;
                typeScope.AddTypeMapping(mapping);
                types.Add(typeName, typeNs, mapping);
                if (a.XmlType != null) {
                    mapping.IncludeInSchema = a.XmlType.IncludeInSchema;
                }
                if (model.TypeDesc.BaseTypeDesc != null) {
                    mapping.BaseMapping = ImportStructLikeMapping((StructModel)modelScope.GetTypeModel(model.Type.BaseType), mapping.Namespace, form);
                    ICollection values = mapping.BaseMapping.LocalElements.Values;
                    foreach (ElementAccessor e in values) {
                        AddUniqueAccessor(mapping.LocalElements, e);
                    }
                    values = mapping.BaseMapping.LocalAttributes.Values;
                    foreach (AttributeAccessor attribute in values) {
                        AddUniqueAccessor(mapping.LocalAttributes, attribute);
                    }
                }
                ArrayList members = new ArrayList();
                TextAccessor textAccesor = null;
                bool hasElements = false;

                foreach (MemberInfo memberInfo in model.GetMemberInfos()) {
                    XmlAttributes memberAttrs = GetAttributes(memberInfo);
                    if (memberAttrs.XmlIgnore) continue;
                    FieldModel fieldModel = model.GetFieldModel(memberInfo);
                    if (fieldModel == null) continue;
                    MemberMapping member = ImportFieldMapping(model, fieldModel, memberAttrs, mapping.Namespace, form);
                    if (member == null) continue;
                    if (mapping.BaseMapping != null) {
                        if (mapping.BaseMapping.Declares(member, mapping.TypeName)) continue;
                    }
                    // add All memeber accessors to the scope accessors
                    AddUniqueAccessor(member, mapping.LocalElements, mapping.LocalAttributes);

                    if (member.Text != null) {
                        if (!member.Text.Mapping.TypeDesc.CanBeTextValue && member.Text.Mapping.IsList)
                            throw new InvalidOperationException(Res.GetString(Res.XmlIllegalTypedTextAttribute, typeName, member.Text.Name, member.Text.Mapping.TypeDesc.FullName));
                        if (textAccesor != null) {
                            throw new InvalidOperationException(Res.GetString(Res.XmlIllegalMultipleText, model.Type.FullName));
                        }
                        textAccesor = member.Text;
                    }
                    if (member.Xmlns != null) {
                        if (mapping.XmlnsMember != null)
                            throw new InvalidOperationException(Res.GetString(Res.XmlMultipleXmlns, model.Type.FullName));
                        mapping.XmlnsMember = member;
                    }
                    if (member.Elements != null && member.Elements.Length != 0) {
                        hasElements = true;
                    }
                    members.Add(member);
                }
                mapping.SetContentModel(textAccesor, hasElements);
                mapping.Members = (MemberMapping[])members.ToArray(typeof(MemberMapping));

                if (mapping.BaseMapping == null) mapping.BaseMapping = GetRootMapping();

                if (mapping.XmlnsMember != null && mapping.BaseMapping.HasXmlnsMember)
                    throw new InvalidOperationException(Res.GetString(Res.XmlMultipleXmlns, model.Type.FullName));

                IncludeTypes(model.Type);
            }
            return mapping;
        }
 private bool InitializeStructMembers(StructMapping mapping, StructModel model, bool openModel, string typeName, RecursionLimiter limiter)
 {
     if (!mapping.IsFullyInitialized)
     {
         if (model.TypeDesc.BaseTypeDesc != null)
         {
             TypeModel typeModel = this.modelScope.GetTypeModel(model.Type.BaseType, false);
             if (!(typeModel is StructModel))
             {
                 throw new NotSupportedException(Res.GetString("XmlUnsupportedInheritance", new object[] { model.Type.BaseType.FullName }));
             }
             StructMapping mapping2 = this.ImportStructLikeMapping((StructModel) typeModel, mapping.Namespace, openModel, null, limiter);
             int index = limiter.DeferredWorkItems.IndexOf(mapping2);
             if (index >= 0)
             {
                 if (!limiter.DeferredWorkItems.Contains(mapping))
                 {
                     limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
                 }
                 int num2 = limiter.DeferredWorkItems.Count - 1;
                 if (index < num2)
                 {
                     ImportStructWorkItem item = limiter.DeferredWorkItems[index];
                     limiter.DeferredWorkItems[index] = limiter.DeferredWorkItems[num2];
                     limiter.DeferredWorkItems[num2] = item;
                 }
                 return false;
             }
             mapping.BaseMapping = mapping2;
             foreach (AttributeAccessor accessor in mapping.BaseMapping.LocalAttributes.Values)
             {
                 AddUniqueAccessor(mapping.LocalAttributes, accessor);
             }
             if (!mapping.BaseMapping.HasExplicitSequence())
             {
                 foreach (ElementAccessor accessor2 in mapping.BaseMapping.LocalElements.Values)
                 {
                     AddUniqueAccessor(mapping.LocalElements, accessor2);
                 }
             }
         }
         ArrayList list = new ArrayList();
         TextAccessor text = null;
         bool hasElements = false;
         bool isSequence = false;
         foreach (MemberInfo info in model.GetMemberInfos())
         {
             if ((info.MemberType & (MemberTypes.Property | MemberTypes.Field)) != 0)
             {
                 XmlAttributes a = this.GetAttributes(info);
                 if (!a.XmlIgnore)
                 {
                     FieldModel fieldModel = model.GetFieldModel(info);
                     if (fieldModel != null)
                     {
                         try
                         {
                             MemberMapping member = this.ImportFieldMapping(model, fieldModel, a, mapping.Namespace, limiter);
                             if ((member != null) && ((mapping.BaseMapping == null) || !mapping.BaseMapping.Declares(member, mapping.TypeName)))
                             {
                                 isSequence |= member.IsSequence;
                                 AddUniqueAccessor(member, mapping.LocalElements, mapping.LocalAttributes, isSequence);
                                 if (member.Text != null)
                                 {
                                     if (!member.Text.Mapping.TypeDesc.CanBeTextValue && member.Text.Mapping.IsList)
                                     {
                                         throw new InvalidOperationException(Res.GetString("XmlIllegalTypedTextAttribute", new object[] { typeName, member.Text.Name, member.Text.Mapping.TypeDesc.FullName }));
                                     }
                                     if (text != null)
                                     {
                                         throw new InvalidOperationException(Res.GetString("XmlIllegalMultipleText", new object[] { model.Type.FullName }));
                                     }
                                     text = member.Text;
                                 }
                                 if (member.Xmlns != null)
                                 {
                                     if (mapping.XmlnsMember != null)
                                     {
                                         throw new InvalidOperationException(Res.GetString("XmlMultipleXmlns", new object[] { model.Type.FullName }));
                                     }
                                     mapping.XmlnsMember = member;
                                 }
                                 if ((member.Elements != null) && (member.Elements.Length != 0))
                                 {
                                     hasElements = true;
                                 }
                                 list.Add(member);
                             }
                         }
                         catch (Exception exception)
                         {
                             if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException))
                             {
                                 throw;
                             }
                             throw this.CreateMemberReflectionException(fieldModel, exception);
                         }
                     }
                 }
             }
         }
         mapping.SetContentModel(text, hasElements);
         if (isSequence)
         {
             Hashtable hashtable = new Hashtable();
             for (int i = 0; i < list.Count; i++)
             {
                 MemberMapping mapping4 = (MemberMapping) list[i];
                 if (mapping4.IsParticle)
                 {
                     if (!mapping4.IsSequence)
                     {
                         throw new InvalidOperationException(Res.GetString("XmlSequenceInconsistent", new object[] { "Order", mapping4.Name }));
                     }
                     if (hashtable[mapping4.SequenceId] != null)
                     {
                         throw new InvalidOperationException(Res.GetString("XmlSequenceUnique", new object[] { mapping4.SequenceId.ToString(CultureInfo.InvariantCulture), "Order", mapping4.Name }));
                     }
                     hashtable[mapping4.SequenceId] = mapping4;
                 }
             }
             list.Sort(new MemberMappingComparer());
         }
         mapping.Members = (MemberMapping[]) list.ToArray(typeof(MemberMapping));
         if (mapping.BaseMapping == null)
         {
             mapping.BaseMapping = this.GetRootMapping();
         }
         if ((mapping.XmlnsMember != null) && mapping.BaseMapping.HasXmlnsMember)
         {
             throw new InvalidOperationException(Res.GetString("XmlMultipleXmlns", new object[] { model.Type.FullName }));
         }
         this.IncludeTypes(model.Type, limiter);
         this.typeScope.AddTypeMapping(mapping);
         if (openModel)
         {
             mapping.IsOpenModel = true;
         }
     }
     return true;
 }