12 private enum ImportContext
19 private TypeScope typeScope;
25 private NameTable types =
new NameTable();
27 private NameTable nullables =
new NameTable();
29 private NameTable elements =
new NameTable();
31 private NameTable xsdAttributes;
37 private NameTable serializables;
39 private StructMapping root;
41 private string defaultNs;
43 private ModelScope modelScope;
45 private int arrayNestingLevel;
49 private string savedArrayNamespace;
51 private int choiceNum = 1;
62 : this(null, defaultNamespace)
69 : this(attributeOverrides, null)
78 if (defaultNamespace ==
null)
80 defaultNamespace =
string.Empty;
82 if (attributeOverrides ==
null)
86 this.attributeOverrides = attributeOverrides;
87 defaultNs = defaultNamespace;
88 typeScope =
new TypeScope();
89 modelScope =
new ModelScope(typeScope);
102 for (
int i = 0; i < customAttributes.Length; i++)
118 int num = arrayNestingLevel;
120 string text = savedArrayNamespace;
121 arrayNestingLevel = 0;
122 savedArrayItemAttributes =
null;
123 savedArrayNamespace =
null;
125 if (typeMapping.IsAnonymousType && !typeMapping.TypeDesc.IsSpecial)
129 arrayNestingLevel = num;
130 savedArrayItemAttributes = xmlArrayItemAttributes;
131 savedArrayNamespace = text;
171 XmlTypeMapping xmlTypeMapping =
new XmlTypeMapping(typeScope, ImportElement(modelScope.GetTypeModel(type), root, defaultNamespace,
new RecursionLimiter()));
172 xmlTypeMapping.SetKeyInternal(
XmlMapping.GenerateKey(type, root, defaultNamespace));
173 xmlTypeMapping.GenerateSerializer =
true;
174 return xmlTypeMapping;
200 return ImportMembersMapping(elementName, ns, members, hasWrapperElement, rpc, openModel:
false);
233 ElementAccessor elementAccessor =
new ElementAccessor();
234 elementAccessor.Name = ((elementName ==
null || elementName.Length == 0) ? elementName :
XmlConvert.
EncodeLocalName(elementName));
235 elementAccessor.Namespace = ns;
236 MembersMapping membersMapping = (MembersMapping)(elementAccessor.Mapping =
ImportMembersMapping(members, ns, hasWrapperElement, rpc, openModel,
new RecursionLimiter()));
240 if (hasWrapperElement)
242 elementAccessor = (ElementAccessor)ReconcileAccessor(elementAccessor, elements);
246 MemberMapping[] members2 = membersMapping.Members;
247 foreach (MemberMapping memberMapping
in members2)
249 if (memberMapping.Elements !=
null && memberMapping.Elements.Length != 0)
251 memberMapping.Elements[0] = (ElementAccessor)ReconcileAccessor(memberMapping.Elements[0], elements);
257 xmlMembersMapping.GenerateSerializer =
true;
258 return xmlMembersMapping;
264 if (xmlAttributes !=
null)
266 return xmlAttributes;
268 if (canBeSimpleType && TypeScope.IsKnownType(type))
270 return defaultAttributes;
272 return new XmlAttributes(type);
275 private XmlAttributes GetAttributes(
MemberInfo memberInfo)
277 XmlAttributes xmlAttributes = attributeOverrides[memberInfo.
DeclaringType, memberInfo.
Name];
278 if (xmlAttributes !=
null)
280 return xmlAttributes;
282 return new XmlAttributes(memberInfo);
285 private ElementAccessor ImportElement(TypeModel model, XmlRootAttribute root,
string defaultNamespace, RecursionLimiter limiter)
287 XmlAttributes attributes = GetAttributes(model.Type, canBeSimpleType:
true);
290 root = attributes.XmlRoot;
292 string text = root?.Namespace;
295 text = defaultNamespace;
301 arrayNestingLevel = -1;
302 savedArrayItemAttributes =
null;
303 savedArrayNamespace =
null;
304 ElementAccessor elementAccessor = CreateElementAccessor(
ImportTypeMapping(model, text, ImportContext.Element,
string.Empty, attributes, limiter), text);
307 if (root.ElementName.Length > 0)
309 elementAccessor.Name = XmlConvert.EncodeLocalName(root.ElementName);
311 if (root.IsNullableSpecified && !root.IsNullable && model.TypeDesc.IsOptionalValue)
313 throw new InvalidOperationException(Res.GetString(
"XmlInvalidNotNullable", model.TypeDesc.BaseTypeDesc.FullName,
"XmlRoot"));
315 elementAccessor.IsNullable = (root.IsNullableSpecified ? root.IsNullable : (model.TypeDesc.IsNullable || model.TypeDesc.IsOptionalValue));
316 CheckNullable(elementAccessor.IsNullable, model.TypeDesc, elementAccessor.Mapping);
320 elementAccessor.IsNullable = (model.TypeDesc.IsNullable || model.TypeDesc.IsOptionalValue);
323 return (ElementAccessor)ReconcileAccessor(elementAccessor, elements);
326 private static string GetMappingName(Mapping mapping)
328 if (mapping is MembersMapping)
332 if (mapping is TypeMapping)
334 return ((TypeMapping)mapping).TypeDesc.FullName;
336 throw new ArgumentException(Res.GetString(
"XmlInternalError"),
"mapping");
339 private ElementAccessor ReconcileLocalAccessor(ElementAccessor accessor,
string ns)
341 if (accessor.Namespace == ns)
345 return (ElementAccessor)ReconcileAccessor(accessor, elements);
348 private Accessor ReconcileAccessor(Accessor accessor, NameTable accessors)
350 if (accessor.Any && accessor.Name.Length == 0)
354 Accessor accessor2 = (Accessor)accessors[accessor.Name, accessor.Namespace];
355 if (accessor2 ==
null)
357 accessor.IsTopLevelInSchema =
true;
358 accessors.Add(accessor.Name, accessor.Namespace, accessor);
361 if (accessor2.Mapping == accessor.Mapping)
365 if (!(accessor.Mapping is MembersMapping) && !(accessor2.Mapping is MembersMapping) && (accessor.Mapping.TypeDesc == accessor2.Mapping.TypeDesc || (accessor2.Mapping is NullableMapping && accessor.Mapping.TypeDesc == ((NullableMapping)accessor2.Mapping).BaseMapping.TypeDesc) || (accessor.Mapping is NullableMapping && ((NullableMapping)accessor.Mapping).BaseMapping.TypeDesc == accessor2.Mapping.TypeDesc)))
373 throw new InvalidOperationException(Res.GetString(
"XmlCannotReconcileAccessorDefault", accessor.Name, accessor.Namespace, text, text2));
375 if (accessor.Mapping is MembersMapping || accessor2.Mapping is MembersMapping)
377 throw new InvalidOperationException(Res.GetString(
"XmlMethodTypeNameConflict", accessor.Name, accessor.Namespace));
379 if (accessor.Mapping is ArrayMapping)
381 if (!(accessor2.Mapping is ArrayMapping))
383 throw new InvalidOperationException(Res.GetString(
"XmlCannotReconcileAccessor", accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping)));
385 ArrayMapping arrayMapping = (ArrayMapping)accessor.Mapping;
386 ArrayMapping arrayMapping2 = arrayMapping.IsAnonymousType ?
null : ((ArrayMapping)types[accessor2.Mapping.TypeName, accessor2.Mapping.Namespace]);
387 ArrayMapping next = arrayMapping2;
388 while (arrayMapping2 !=
null)
390 if (arrayMapping2 == accessor.Mapping)
394 arrayMapping2 = arrayMapping2.Next;
396 arrayMapping.Next = next;
397 if (!arrayMapping.IsAnonymousType)
399 types[accessor2.Mapping.TypeName, accessor2.Mapping.Namespace] = arrayMapping;
403 if (accessor is AttributeAccessor)
405 throw new InvalidOperationException(Res.GetString(
"XmlCannotReconcileAttributeAccessor", accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping)));
407 throw new InvalidOperationException(Res.GetString(
"XmlCannotReconcileAccessor", accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping)));
410 private Exception CreateReflectionException(
string context, Exception e)
412 return new InvalidOperationException(Res.GetString(
"XmlReflectionError", context), e);
415 private Exception CreateTypeReflectionException(
string context, Exception e)
417 return new InvalidOperationException(Res.GetString(
"XmlTypeReflectionError", context), e);
420 private Exception CreateMemberReflectionException(FieldModel model, Exception e)
422 return new InvalidOperationException(Res.GetString(model.IsProperty ?
"XmlPropertyReflectionError" :
"XmlFieldReflectionError", model.Name), e);
425 private TypeMapping
ImportTypeMapping(TypeModel model,
string ns, ImportContext context,
string dataType, XmlAttributes a, RecursionLimiter limiter)
427 return ImportTypeMapping(model, ns, context, dataType, a, repeats:
false, openModel:
false, limiter);
430 private TypeMapping
ImportTypeMapping(TypeModel model,
string ns, ImportContext context,
string dataType, XmlAttributes a,
bool repeats,
bool openModel, RecursionLimiter limiter)
434 if (dataType.Length > 0)
436 TypeDesc typeDesc = TypeScope.IsOptionalValue(model.Type) ? model.TypeDesc.BaseTypeDesc : model.TypeDesc;
437 if (!typeDesc.IsPrimitive)
439 throw new InvalidOperationException(Res.GetString(
"XmlInvalidDataTypeUsage", dataType,
"XmlElementAttribute.DataType"));
441 TypeDesc typeDesc2 = typeScope.GetTypeDesc(dataType,
"http://www.w3.org/2001/XMLSchema");
442 if (typeDesc2 ==
null)
444 throw new InvalidOperationException(Res.GetString(
"XmlInvalidXsdDataType", dataType,
"XmlElementAttribute.DataType",
new XmlQualifiedName(dataType,
"http://www.w3.org/2001/XMLSchema").ToString()));
446 if (typeDesc.FullName != typeDesc2.FullName)
448 throw new InvalidOperationException(Res.GetString(
"XmlDataTypeMismatch", dataType,
"XmlElementAttribute.DataType", typeDesc.FullName));
453 a = GetAttributes(model.Type, canBeSimpleType:
false);
455 if ((a.XmlFlags & (XmlAttributeFlags)(-193)) != 0)
457 throw new InvalidOperationException(Res.GetString(
"XmlInvalidTypeAttributes", model.Type.FullName));
459 switch (model.TypeDesc.Kind)
462 return ImportEnumMapping((EnumModel)model, ns, repeats);
463 case TypeKind.Primitive:
466 throw InvalidAttributeUseException(model.Type);
468 return ImportPrimitiveMapping((PrimitiveModel)model, context, dataType, repeats);
470 case TypeKind.Collection:
471 case TypeKind.Enumerable:
473 if (context != ImportContext.Element)
475 throw UnsupportedException(model.TypeDesc, context);
478 ArrayMapping result = ImportArrayLikeMapping((ArrayModel)model, ns, limiter);
483 case TypeKind.Struct:
485 if (context != ImportContext.Element)
487 throw UnsupportedException(model.TypeDesc, context);
489 if (model.TypeDesc.IsOptionalValue)
491 TypeDesc typeDesc3 =
string.IsNullOrEmpty(dataType) ? model.TypeDesc.BaseTypeDesc : typeScope.GetTypeDesc(dataType,
"http://www.w3.org/2001/XMLSchema");
492 string typeName = (typeDesc3.DataType ==
null) ? typeDesc3.Name : typeDesc3.DataType.Name;
493 TypeMapping typeMapping = GetTypeMapping(typeName, ns, typeDesc3, types,
null);
494 if (typeMapping ==
null)
496 typeMapping =
ImportTypeMapping(modelScope.GetTypeModel(model.TypeDesc.BaseTypeDesc.Type), ns, context, dataType,
null, repeats, openModel, limiter);
498 return CreateNullableMapping(typeMapping, model.TypeDesc.Type);
500 return ImportStructLikeMapping((StructModel)model, ns, openModel, a, limiter);
502 if (model.TypeDesc.Kind == TypeKind.Serializable)
504 if ((a.XmlFlags & (XmlAttributeFlags)(-65)) != 0)
506 throw new InvalidOperationException(Res.GetString(
"XmlSerializableAttributes", model.TypeDesc.FullName, typeof(XmlSchemaProviderAttribute).Name));
509 else if (a.XmlFlags != 0)
511 throw InvalidAttributeUseException(model.Type);
513 if (!model.TypeDesc.IsSpecial)
515 throw UnsupportedException(model.TypeDesc, context);
517 return ImportSpecialMapping(model.Type, model.TypeDesc, ns, context, limiter);
526 throw CreateTypeReflectionException(model.TypeDesc.FullName, ex);
530 internal static MethodInfo GetMethodFromSchemaProvider(XmlSchemaProviderAttribute provider, Type type)
536 if (provider.MethodName ==
null)
538 throw new ArgumentNullException(
"MethodName");
540 if (!CodeGenerator.IsValidLanguageIndependentIdentifier(provider.MethodName))
542 throw new ArgumentException(Res.GetString(
"XmlGetSchemaMethodName", provider.MethodName),
"MethodName");
550 throw new InvalidOperationException(Res.GetString(
"XmlGetSchemaMethodMissing", provider.MethodName, typeof(
XmlSchemaSet).Name, type.FullName));
554 throw new InvalidOperationException(Res.GetString(
"XmlGetSchemaMethodReturnType", type.Name, provider.MethodName, typeof(XmlSchemaProviderAttribute).Name, typeof(XmlQualifiedName).FullName, typeof(
XmlSchemaType).FullName));
559 private SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc,
string ns, ImportContext context, RecursionLimiter limiter)
561 if (specials ==
null)
565 SpecialMapping specialMapping = (SpecialMapping)specials[type];
566 if (specialMapping !=
null)
568 CheckContext(specialMapping.TypeDesc, context);
569 return specialMapping;
571 if (typeDesc.Kind == TypeKind.Serializable)
573 SerializableMapping serializableMapping =
null;
574 object[] customAttributes = type.GetCustomAttributes(typeof(XmlSchemaProviderAttribute), inherit:
false);
575 if (customAttributes.Length != 0)
577 XmlSchemaProviderAttribute xmlSchemaProviderAttribute = (XmlSchemaProviderAttribute)customAttributes[0];
578 MethodInfo methodFromSchemaProvider = GetMethodFromSchemaProvider(xmlSchemaProviderAttribute, type);
579 serializableMapping =
new SerializableMapping(methodFromSchemaProvider, xmlSchemaProviderAttribute.IsAny, ns);
580 XmlQualifiedName xsiType = serializableMapping.XsiType;
581 if (xsiType !=
null && !xsiType.IsEmpty)
583 if (serializables ==
null)
585 serializables =
new NameTable();
587 SerializableMapping serializableMapping2 = (SerializableMapping)serializables[xsiType];
588 if (serializableMapping2 !=
null)
590 if (serializableMapping2.Type ==
null)
592 serializableMapping = serializableMapping2;
594 else if (serializableMapping2.Type != type)
596 SerializableMapping next = serializableMapping2.Next;
597 serializableMapping2.Next = serializableMapping;
598 serializableMapping.Next = next;
606 SetBase(serializableMapping, xsdType.DerivedFrom);
608 serializables[xsiType] = serializableMapping;
610 serializableMapping.TypeName = xsiType.Name;
611 serializableMapping.Namespace = xsiType.Namespace;
613 serializableMapping.TypeDesc = typeDesc;
614 serializableMapping.Type = type;
619 serializableMapping =
new SerializableMapping();
620 serializableMapping.TypeDesc = typeDesc;
621 serializableMapping.Type = type;
623 specialMapping = serializableMapping;
627 specialMapping =
new SpecialMapping();
628 specialMapping.TypeDesc = typeDesc;
630 CheckContext(typeDesc, context);
631 specials.
Add(type, specialMapping);
632 typeScope.AddTypeMapping(specialMapping);
633 return specialMapping;
636 internal static void ValidationCallbackWithErrorCode(
object sender,
ValidationEventArgs args)
640 throw new InvalidOperationException(Res.GetString(
"XmlSerializableSchemaError", typeof(IXmlSerializable).Name, args.
Message));
644 internal void SetBase(SerializableMapping mapping, XmlQualifiedName baseQname)
646 if (!baseQname.IsEmpty && !(baseQname.Namespace ==
"http://www.w3.org/2001/XMLSchema"))
650 if (arrayList.
Count == 0)
652 throw new InvalidOperationException(Res.GetString(
"XmlMissingSchema", baseQname.Namespace));
654 if (arrayList.
Count > 1)
656 throw new InvalidOperationException(Res.GetString(
"XmlGetSchemaInclude", baseQname.Namespace, typeof(IXmlSerializable).Name,
"GetSchema"));
660 xmlSchemaType = ((xmlSchemaType.Redefined !=
null) ? xmlSchemaType.Redefined : xmlSchemaType);
661 if (serializables[baseQname] ==
null)
663 SerializableMapping serializableMapping =
new SerializableMapping(baseQname, schemas);
664 SetBase(serializableMapping, xmlSchemaType.DerivedFrom);
665 serializables.Add(baseQname, serializableMapping);
667 mapping.SetBaseMapping((SerializableMapping)serializables[baseQname]);
671 private static string GetContextName(ImportContext context)
675 case ImportContext.Element:
677 case ImportContext.Attribute:
679 case ImportContext.Text:
682 throw new ArgumentException(Res.GetString(
"XmlInternalError"),
"context");
686 private static Exception InvalidAttributeUseException(Type type)
688 return new InvalidOperationException(Res.GetString(
"XmlInvalidAttributeUse", type.FullName));
691 private static Exception UnsupportedException(TypeDesc typeDesc, ImportContext context)
693 return new InvalidOperationException(Res.GetString(
"XmlIllegalTypeContext", typeDesc.FullName, GetContextName(context)));
696 private StructMapping CreateRootMapping()
698 TypeDesc typeDesc = typeScope.GetTypeDesc(typeof(
object));
699 StructMapping structMapping =
new StructMapping();
700 structMapping.TypeDesc = typeDesc;
701 structMapping.TypeName =
"anyType";
702 structMapping.Namespace =
"http://www.w3.org/2001/XMLSchema";
703 structMapping.Members =
new MemberMapping[0];
704 structMapping.IncludeInSchema =
false;
705 return structMapping;
708 private NullableMapping CreateNullableMapping(TypeMapping baseMapping, Type type)
710 TypeDesc nullableTypeDesc = baseMapping.TypeDesc.GetNullableTypeDesc(type);
711 TypeMapping typeMapping = baseMapping.IsAnonymousType ? ((TypeMapping)anonymous[type]) : ((TypeMapping)nullables[baseMapping.TypeName, baseMapping.Namespace]);
712 NullableMapping nullableMapping;
713 if (typeMapping !=
null)
715 if (typeMapping is NullableMapping)
717 nullableMapping = (NullableMapping)typeMapping;
718 if (nullableMapping.BaseMapping is PrimitiveMapping && baseMapping is PrimitiveMapping)
720 return nullableMapping;
722 if (nullableMapping.BaseMapping == baseMapping)
724 return nullableMapping;
726 throw new InvalidOperationException(Res.GetString(
"XmlTypesDuplicate", nullableTypeDesc.FullName, typeMapping.TypeDesc.FullName, nullableTypeDesc.Name, typeMapping.Namespace));
728 throw new InvalidOperationException(Res.GetString(
"XmlTypesDuplicate", nullableTypeDesc.FullName, typeMapping.TypeDesc.FullName, nullableTypeDesc.Name, typeMapping.Namespace));
730 nullableMapping =
new NullableMapping();
731 nullableMapping.BaseMapping = baseMapping;
732 nullableMapping.TypeDesc = nullableTypeDesc;
733 nullableMapping.TypeName = baseMapping.TypeName;
734 nullableMapping.Namespace = baseMapping.Namespace;
735 nullableMapping.IncludeInSchema = baseMapping.IncludeInSchema;
736 if (!baseMapping.IsAnonymousType)
738 nullables.Add(baseMapping.TypeName, baseMapping.Namespace, nullableMapping);
742 anonymous[type] = nullableMapping;
744 typeScope.AddTypeMapping(nullableMapping);
745 return nullableMapping;
748 private StructMapping GetRootMapping()
752 root = CreateRootMapping();
753 typeScope.AddTypeMapping(root);
758 private TypeMapping GetTypeMapping(
string typeName,
string ns, TypeDesc typeDesc, NameTable typeLib, Type type)
760 TypeMapping typeMapping = (typeName !=
null && typeName.Length != 0) ? ((TypeMapping)typeLib[typeName, ns]) : ((type ==
null) ? null : ((TypeMapping)anonymous[type]));
761 if (typeMapping ==
null)
765 if (!typeMapping.IsAnonymousType && typeMapping.TypeDesc != typeDesc)
767 throw new InvalidOperationException(Res.GetString(
"XmlTypesDuplicate", typeDesc.FullName, typeMapping.TypeDesc.FullName, typeName, ns));
772 private StructMapping ImportStructLikeMapping(StructModel model,
string ns,
bool openModel, XmlAttributes a, RecursionLimiter limiter)
774 if (model.TypeDesc.Kind == TypeKind.Root)
776 return GetRootMapping();
780 a = GetAttributes(model.Type, canBeSimpleType:
false);
783 if (a.XmlType !=
null && a.XmlType.Namespace !=
null)
785 text = a.XmlType.Namespace;
787 else if (a.XmlRoot !=
null && a.XmlRoot.Namespace !=
null)
789 text = a.XmlRoot.Namespace;
791 string name = IsAnonymousType(a, ns) ? null : XsdTypeName(model.Type, a, model.TypeDesc.Name);
792 name = XmlConvert.EncodeLocalName(name);
793 StructMapping structMapping = (StructMapping)GetTypeMapping(name, text, model.TypeDesc, types, model.Type);
794 if (structMapping ==
null)
796 structMapping =
new StructMapping();
797 structMapping.TypeDesc = model.TypeDesc;
798 structMapping.Namespace = text;
799 structMapping.TypeName = name;
800 if (!structMapping.IsAnonymousType)
802 types.Add(name, text, structMapping);
806 anonymous[model.Type] = structMapping;
808 if (a.XmlType !=
null)
810 structMapping.IncludeInSchema = a.XmlType.IncludeInSchema;
812 if (limiter.IsExceededLimit)
814 limiter.DeferredWorkItems.
Add(
new ImportStructWorkItem(model, structMapping));
815 return structMapping;
818 InitializeStructMembers(structMapping, model, openModel, name, limiter);
819 while (limiter.DeferredWorkItems.Count > 0)
821 int index = limiter.DeferredWorkItems.Count - 1;
822 ImportStructWorkItem importStructWorkItem = limiter.DeferredWorkItems[index];
823 if (InitializeStructMembers(importStructWorkItem.Mapping, importStructWorkItem.Model, openModel, name, limiter))
825 limiter.DeferredWorkItems.RemoveAt(index);
830 return structMapping;
833 private bool InitializeStructMembers(StructMapping mapping, StructModel model,
bool openModel,
string typeName, RecursionLimiter limiter)
835 if (mapping.IsFullyInitialized)
839 if (model.TypeDesc.BaseTypeDesc !=
null)
841 TypeModel typeModel = modelScope.GetTypeModel(model.Type.BaseType, directReference:
false);
842 if (!(typeModel is StructModel))
844 throw new NotSupportedException(Res.GetString(
"XmlUnsupportedInheritance", model.Type.BaseType.FullName));
846 StructMapping structMapping = ImportStructLikeMapping((StructModel)typeModel, mapping.Namespace, openModel,
null, limiter);
847 int num = limiter.DeferredWorkItems.IndexOf(structMapping);
850 if (!limiter.DeferredWorkItems.Contains(mapping))
852 limiter.DeferredWorkItems.Add(
new ImportStructWorkItem(model, mapping));
854 int num2 = limiter.DeferredWorkItems.Count - 1;
857 ImportStructWorkItem value = limiter.DeferredWorkItems[num];
858 limiter.DeferredWorkItems[num] = limiter.DeferredWorkItems[num2];
859 limiter.DeferredWorkItems[num2] = value;
863 mapping.BaseMapping = structMapping;
864 ICollection values = mapping.BaseMapping.LocalAttributes.Values;
865 foreach (AttributeAccessor item
in values)
867 AddUniqueAccessor(mapping.LocalAttributes, item);
869 if (!mapping.BaseMapping.HasExplicitSequence())
871 values = mapping.BaseMapping.LocalElements.Values;
872 foreach (ElementAccessor item2
in values)
874 AddUniqueAccessor(mapping.LocalElements, item2);
879 TextAccessor textAccessor =
null;
880 bool hasElements =
false;
882 MemberInfo[] memberInfos = model.GetMemberInfos();
883 foreach (
MemberInfo memberInfo
in memberInfos)
889 XmlAttributes attributes = GetAttributes(memberInfo);
890 if (!attributes.XmlIgnore)
892 FieldModel fieldModel = model.GetFieldModel(memberInfo);
893 if (fieldModel !=
null)
897 MemberMapping memberMapping = ImportFieldMapping(model, fieldModel, attributes, mapping.Namespace, limiter);
898 if (memberMapping !=
null && (mapping.BaseMapping ==
null || !mapping.BaseMapping.Declares(memberMapping, mapping.TypeName)))
900 flag |= memberMapping.IsSequence;
901 AddUniqueAccessor(memberMapping, mapping.LocalElements, mapping.LocalAttributes, flag);
902 if (memberMapping.Text !=
null)
904 if (!memberMapping.Text.Mapping.TypeDesc.CanBeTextValue && memberMapping.Text.Mapping.IsList)
906 throw new InvalidOperationException(Res.GetString(
"XmlIllegalTypedTextAttribute", typeName, memberMapping.Text.Name, memberMapping.Text.Mapping.TypeDesc.FullName));
908 if (textAccessor !=
null)
910 throw new InvalidOperationException(Res.GetString(
"XmlIllegalMultipleText", model.Type.FullName));
912 textAccessor = memberMapping.Text;
914 if (memberMapping.Xmlns !=
null)
916 if (mapping.XmlnsMember !=
null)
918 throw new InvalidOperationException(Res.GetString(
"XmlMultipleXmlns", model.Type.FullName));
920 mapping.XmlnsMember = memberMapping;
922 if (memberMapping.Elements !=
null && memberMapping.Elements.Length != 0)
926 arrayList.
Add(memberMapping);
935 throw CreateMemberReflectionException(fieldModel, ex);
940 mapping.SetContentModel(textAccessor, hasElements);
944 for (
int j = 0; j < arrayList.
Count; j++)
946 MemberMapping memberMapping2 = (MemberMapping)arrayList[j];
947 if (memberMapping2.IsParticle)
949 if (!memberMapping2.IsSequence)
951 throw new InvalidOperationException(Res.GetString(
"XmlSequenceInconsistent",
"Order", memberMapping2.Name));
953 if (hashtable[memberMapping2.SequenceId] !=
null)
955 throw new InvalidOperationException(Res.GetString(
"XmlSequenceUnique", memberMapping2.SequenceId.ToString(
CultureInfo.
InvariantCulture),
"Order", memberMapping2.Name));
957 hashtable[memberMapping2.SequenceId] = memberMapping2;
960 arrayList.
Sort(
new MemberMappingComparer());
962 mapping.Members = (MemberMapping[])arrayList.
ToArray(typeof(MemberMapping));
963 if (mapping.BaseMapping ==
null)
965 mapping.BaseMapping = GetRootMapping();
967 if (mapping.XmlnsMember !=
null && mapping.BaseMapping.HasXmlnsMember)
969 throw new InvalidOperationException(Res.GetString(
"XmlMultipleXmlns", model.Type.FullName));
972 typeScope.AddTypeMapping(mapping);
975 mapping.IsOpenModel =
true;
980 private static bool IsAnonymousType(XmlAttributes a,
string contextNs)
982 if (a.XmlType !=
null && a.XmlType.AnonymousType)
984 string @
namespace = a.XmlType.Namespace;
985 if (!
string.IsNullOrEmpty(@
namespace))
987 return @
namespace == contextNs;
994 internal string XsdTypeName(Type type)
996 if (type == typeof(
object))
1000 TypeDesc typeDesc = typeScope.GetTypeDesc(type);
1001 if (typeDesc.IsPrimitive && typeDesc.DataType !=
null && typeDesc.DataType.Name !=
null && typeDesc.DataType.Name.Length > 0)
1003 return typeDesc.DataType.Name;
1005 return XsdTypeName(type, GetAttributes(type, canBeSimpleType:
false), typeDesc.Name);
1008 internal string XsdTypeName(Type type, XmlAttributes a,
string name)
1011 if (a.XmlType !=
null && a.XmlType.TypeName.Length > 0)
1013 text = a.XmlType.TypeName;
1015 if (type.IsGenericType && text.IndexOf(
'{') >= 0)
1017 Type genericTypeDefinition = type.GetGenericTypeDefinition();
1018 Type[] genericArguments = genericTypeDefinition.GetGenericArguments();
1019 Type[] genericArguments2 = type.GetGenericArguments();
1020 for (
int i = 0; i < genericArguments.Length; i++)
1022 string text2 =
"{" + genericArguments[i] +
"}";
1023 if (text.Contains(text2))
1025 text = text.Replace(text2, XsdTypeName(genericArguments2[i]));
1026 if (text.IndexOf(
'{') < 0)
1036 private static int CountAtLevel(XmlArrayItemAttributes attributes,
int level)
1039 for (
int i = 0; i < attributes.Count; i++)
1041 if (attributes[i].NestingLevel == level)
1049 private void SetArrayMappingType(ArrayMapping mapping,
string defaultNs, Type type)
1051 XmlAttributes attributes = GetAttributes(type, canBeSimpleType:
false);
1052 if (IsAnonymousType(attributes, defaultNs))
1054 mapping.TypeName =
null;
1055 mapping.Namespace = defaultNs;
1058 ElementAccessor elementAccessor =
null;
1059 TypeMapping typeMapping;
1060 if (mapping.Elements.Length == 1)
1062 elementAccessor = mapping.Elements[0];
1063 typeMapping = elementAccessor.Mapping;
1072 if (attributes.XmlType !=
null)
1074 text = attributes.XmlType.Namespace;
1075 name = XsdTypeName(type, attributes, attributes.XmlType.TypeName);
1076 name = XmlConvert.EncodeLocalName(name);
1077 flag = (name ==
null);
1079 else if (typeMapping is EnumMapping)
1081 text = typeMapping.Namespace;
1082 name = typeMapping.DefaultElementName;
1084 else if (typeMapping is PrimitiveMapping)
1087 name = typeMapping.TypeDesc.DataType.Name;
1089 else if (typeMapping is StructMapping && typeMapping.TypeDesc.IsRoot)
1094 else if (typeMapping !=
null)
1096 text = ((typeMapping.Namespace ==
"http://www.w3.org/2001/XMLSchema") ? defaultNs : typeMapping.Namespace);
1097 name = typeMapping.DefaultElementName;
1102 name =
"Choice" + choiceNum++;
1108 if (elementAccessor !=
null)
1110 text = elementAccessor.Namespace;
1116 string text2 = name = (flag ? (
"ArrayOf" + CodeIdentifier.MakePascal(name)) : name);
1118 TypeMapping typeMapping2 = (TypeMapping)types[text2, text];
1119 while (typeMapping2 !=
null)
1121 if (typeMapping2 is ArrayMapping)
1123 ArrayMapping arrayMapping = (ArrayMapping)typeMapping2;
1124 if (AccessorMapping.ElementsMatch(arrayMapping.Elements, mapping.Elements))
1130 typeMapping2 = (TypeMapping)types[text2, text];
1133 mapping.TypeName = text2;
1134 mapping.Namespace = text;
1137 private ArrayMapping ImportArrayLikeMapping(ArrayModel model,
string ns, RecursionLimiter limiter)
1139 ArrayMapping arrayMapping =
new ArrayMapping();
1140 arrayMapping.TypeDesc = model.TypeDesc;
1141 if (savedArrayItemAttributes ==
null)
1143 savedArrayItemAttributes =
new XmlArrayItemAttributes();
1145 if (CountAtLevel(savedArrayItemAttributes, arrayNestingLevel) == 0)
1147 savedArrayItemAttributes.
Add(CreateArrayItemAttribute(typeScope.GetTypeDesc(model.Element.Type), arrayNestingLevel));
1149 CreateArrayElementsFromAttributes(arrayMapping, savedArrayItemAttributes, model.Element.Type, (savedArrayNamespace ==
null) ? ns : savedArrayNamespace, limiter);
1150 SetArrayMappingType(arrayMapping, ns, model.Type);
1151 for (
int i = 0; i < arrayMapping.Elements.Length; i++)
1153 arrayMapping.Elements[i] = ReconcileLocalAccessor(arrayMapping.Elements[i], arrayMapping.Namespace);
1156 ArrayMapping arrayMapping2 = (ArrayMapping)types[arrayMapping.TypeName, arrayMapping.Namespace];
1157 if (arrayMapping2 !=
null)
1159 ArrayMapping next = arrayMapping2;
1160 while (arrayMapping2 !=
null)
1162 if (arrayMapping2.TypeDesc == model.TypeDesc)
1164 return arrayMapping2;
1166 arrayMapping2 = arrayMapping2.Next;
1168 arrayMapping.Next = next;
1169 if (!arrayMapping.IsAnonymousType)
1171 types[arrayMapping.TypeName, arrayMapping.Namespace] = arrayMapping;
1175 anonymous[model.Type] = arrayMapping;
1177 return arrayMapping;
1179 typeScope.AddTypeMapping(arrayMapping);
1180 if (!arrayMapping.IsAnonymousType)
1182 types.
Add(arrayMapping.TypeName, arrayMapping.Namespace, arrayMapping);
1186 anonymous[model.Type] = arrayMapping;
1188 return arrayMapping;
1191 private void CheckContext(TypeDesc typeDesc, ImportContext context)
1195 case ImportContext.Element:
1196 if (typeDesc.CanBeElementValue)
1201 case ImportContext.Attribute:
1202 if (typeDesc.CanBeAttributeValue)
1207 case ImportContext.Text:
1208 if (typeDesc.CanBeTextValue || typeDesc.IsEnum || typeDesc.IsPrimitive)
1214 throw new ArgumentException(Res.GetString(
"XmlInternalError"),
"context");
1216 throw UnsupportedException(typeDesc, context);
1219 private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, ImportContext context,
string dataType,
bool repeats)
1221 PrimitiveMapping primitiveMapping =
new PrimitiveMapping();
1222 if (dataType.Length > 0)
1224 primitiveMapping.TypeDesc = typeScope.GetTypeDesc(dataType,
"http://www.w3.org/2001/XMLSchema");
1225 if (primitiveMapping.TypeDesc ==
null)
1227 primitiveMapping.TypeDesc = typeScope.GetTypeDesc(dataType,
"http://microsoft.com/wsdl/types/");
1228 if (primitiveMapping.TypeDesc ==
null)
1230 throw new InvalidOperationException(Res.GetString(
"XmlUdeclaredXsdType", dataType));
1236 primitiveMapping.TypeDesc = model.TypeDesc;
1238 primitiveMapping.TypeName = primitiveMapping.TypeDesc.DataType.Name;
1239 primitiveMapping.Namespace = (primitiveMapping.TypeDesc.IsXsdType ?
"http://www.w3.org/2001/XMLSchema" :
"http://microsoft.com/wsdl/types/");
1240 primitiveMapping.IsList = repeats;
1241 CheckContext(primitiveMapping.TypeDesc, context);
1242 return primitiveMapping;
1245 private EnumMapping ImportEnumMapping(EnumModel model,
string ns,
bool repeats)
1247 XmlAttributes attributes = GetAttributes(model.Type, canBeSimpleType:
false);
1249 if (attributes.XmlType !=
null && attributes.XmlType.Namespace !=
null)
1251 text = attributes.XmlType.Namespace;
1253 string name = IsAnonymousType(attributes, ns) ? null : XsdTypeName(model.Type, attributes, model.TypeDesc.Name);
1254 name = XmlConvert.EncodeLocalName(name);
1255 EnumMapping enumMapping = (EnumMapping)GetTypeMapping(name, text, model.TypeDesc, types, model.Type);
1256 if (enumMapping ==
null)
1258 enumMapping =
new EnumMapping();
1259 enumMapping.TypeDesc = model.TypeDesc;
1260 enumMapping.TypeName = name;
1261 enumMapping.Namespace = text;
1262 enumMapping.IsFlags = model.Type.IsDefined(typeof(FlagsAttribute), inherit:
false);
1263 if (enumMapping.IsFlags && repeats)
1265 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttributeFlagsArray", model.TypeDesc.FullName));
1267 enumMapping.IsList = repeats;
1268 enumMapping.IncludeInSchema = (attributes.XmlType ==
null || attributes.XmlType.IncludeInSchema);
1269 if (!enumMapping.IsAnonymousType)
1271 types.Add(name, text, enumMapping);
1275 anonymous[model.Type] = enumMapping;
1278 for (
int i = 0; i < model.Constants.Length; i++)
1280 ConstantMapping constantMapping = ImportConstantMapping(model.Constants[i]);
1281 if (constantMapping !=
null)
1283 arrayList.
Add(constantMapping);
1286 if (arrayList.
Count == 0)
1288 throw new InvalidOperationException(Res.GetString(
"XmlNoSerializableMembers", model.TypeDesc.FullName));
1290 enumMapping.Constants = (ConstantMapping[])arrayList.
ToArray(typeof(ConstantMapping));
1291 typeScope.AddTypeMapping(enumMapping);
1296 private ConstantMapping ImportConstantMapping(ConstantModel model)
1298 XmlAttributes attributes = GetAttributes(model.FieldInfo);
1299 if (attributes.XmlIgnore)
1303 if ((attributes.XmlFlags & (XmlAttributeFlags)(-2)) != 0)
1305 throw new InvalidOperationException(Res.GetString(
"XmlInvalidConstantAttribute"));
1307 if (attributes.XmlEnum ==
null)
1309 attributes.XmlEnum =
new XmlEnumAttribute();
1311 ConstantMapping constantMapping =
new ConstantMapping();
1312 constantMapping.XmlName = ((attributes.XmlEnum.Name ==
null) ? model.Name : attributes.XmlEnum.Name);
1313 constantMapping.Name = model.Name;
1314 constantMapping.Value = model.Value;
1315 return constantMapping;
1318 private MembersMapping
ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers,
string ns,
bool hasWrapperElement,
bool rpc,
bool openModel, RecursionLimiter limiter)
1320 MembersMapping membersMapping =
new MembersMapping();
1321 membersMapping.TypeDesc = typeScope.GetTypeDesc(typeof(
object[]));
1322 MemberMapping[] array =
new MemberMapping[xmlReflectionMembers.Length];
1323 NameTable nameTable =
new NameTable();
1324 NameTable attributes =
new NameTable();
1325 TextAccessor textAccessor =
null;
1327 for (
int i = 0; i < array.Length; i++)
1331 MemberMapping memberMapping = ImportMemberMapping(xmlReflectionMembers[i], ns, xmlReflectionMembers, rpc, openModel, limiter);
1332 if (!hasWrapperElement && memberMapping.Attribute !=
null)
1336 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitAttributeAttributes"));
1338 throw new InvalidOperationException(Res.GetString(
"XmlInvalidAttributeType",
"XmlAttribute"));
1340 if (rpc && xmlReflectionMembers[i].IsReturnValue)
1344 throw new InvalidOperationException(Res.GetString(
"XmlInvalidReturnPosition"));
1346 memberMapping.IsReturnValue =
true;
1348 array[i] = memberMapping;
1349 flag |= memberMapping.IsSequence;
1350 if (!xmlReflectionMembers[i].XmlAttributes.XmlIgnore)
1352 AddUniqueAccessor(memberMapping, nameTable, attributes, flag);
1354 array[i] = memberMapping;
1355 if (memberMapping.Text !=
null)
1357 if (textAccessor !=
null)
1359 throw new InvalidOperationException(Res.GetString(
"XmlIllegalMultipleTextMembers"));
1361 textAccessor = memberMapping.Text;
1363 if (memberMapping.Xmlns !=
null)
1365 if (membersMapping.XmlnsMember !=
null)
1367 throw new InvalidOperationException(Res.GetString(
"XmlMultipleXmlnsMembers"));
1369 membersMapping.XmlnsMember = memberMapping;
1372 catch (Exception ex)
1378 throw CreateReflectionException(xmlReflectionMembers[i].MemberName, ex);
1383 throw new InvalidOperationException(Res.GetString(
"XmlSequenceMembers",
"Order"));
1385 membersMapping.Members = array;
1386 membersMapping.HasWrapperElement = hasWrapperElement;
1387 return membersMapping;
1390 private MemberMapping ImportMemberMapping(XmlReflectionMember xmlReflectionMember,
string ns, XmlReflectionMember[] xmlReflectionMembers,
bool rpc,
bool openModel, RecursionLimiter limiter)
1393 XmlAttributes xmlAttributes = xmlReflectionMember.XmlAttributes;
1394 TypeDesc typeDesc = typeScope.GetTypeDesc(xmlReflectionMember.MemberType);
1395 if (xmlAttributes.XmlFlags == (XmlAttributeFlags)0)
1397 if (typeDesc.IsArrayLike)
1399 XmlArrayAttribute xmlArrayAttribute = CreateArrayAttribute(typeDesc);
1400 xmlArrayAttribute.ElementName = xmlReflectionMember.MemberName;
1401 xmlArrayAttribute.Namespace = (rpc ? null : ns);
1402 xmlArrayAttribute.Form = form;
1403 xmlAttributes.XmlArray = xmlArrayAttribute;
1407 XmlElementAttribute xmlElementAttribute = CreateElementAttribute(typeDesc);
1408 if (typeDesc.IsStructLike)
1410 XmlAttributes xmlAttributes2 =
new XmlAttributes(xmlReflectionMember.MemberType);
1411 if (xmlAttributes2.XmlRoot !=
null)
1413 if (xmlAttributes2.XmlRoot.ElementName.Length > 0)
1415 xmlElementAttribute.ElementName = xmlAttributes2.XmlRoot.ElementName;
1419 xmlElementAttribute.Namespace =
null;
1420 if (xmlAttributes2.XmlRoot.IsNullableSpecified)
1422 xmlElementAttribute.IsNullable = xmlAttributes2.XmlRoot.IsNullable;
1427 xmlElementAttribute.Namespace = xmlAttributes2.XmlRoot.Namespace;
1428 xmlElementAttribute.IsNullable = xmlAttributes2.XmlRoot.IsNullable;
1432 if (xmlElementAttribute.ElementName.Length == 0)
1434 xmlElementAttribute.ElementName = xmlReflectionMember.MemberName;
1436 if (xmlElementAttribute.Namespace ==
null && !rpc)
1438 xmlElementAttribute.Namespace = ns;
1440 xmlElementAttribute.Form = form;
1441 xmlAttributes.XmlElements.Add(xmlElementAttribute);
1444 else if (xmlAttributes.XmlRoot !=
null)
1446 CheckNullable(xmlAttributes.XmlRoot.IsNullable, typeDesc,
null);
1448 MemberMapping memberMapping =
new MemberMapping();
1449 memberMapping.Name = xmlReflectionMember.MemberName;
1450 bool checkSpecified = FindSpecifiedMember(xmlReflectionMember.MemberName, xmlReflectionMembers) !=
null;
1451 FieldModel fieldModel =
new FieldModel(xmlReflectionMember.MemberName, xmlReflectionMember.MemberType, typeScope.GetTypeDesc(xmlReflectionMember.MemberType), checkSpecified, checkShouldPersist:
false);
1452 memberMapping.CheckShouldPersist = fieldModel.CheckShouldPersist;
1453 memberMapping.CheckSpecified = fieldModel.CheckSpecified;
1454 memberMapping.ReadOnly = fieldModel.ReadOnly;
1455 Type choiceIdentifierType =
null;
1456 if (xmlAttributes.XmlChoiceIdentifier !=
null)
1458 choiceIdentifierType = GetChoiceIdentifierType(xmlAttributes.XmlChoiceIdentifier, xmlReflectionMembers, typeDesc.IsArrayLike, fieldModel.Name);
1460 ImportAccessorMapping(memberMapping, fieldModel, xmlAttributes, ns, choiceIdentifierType, rpc, openModel, limiter);
1461 if (xmlReflectionMember.OverrideIsNullable && memberMapping.Elements.Length != 0)
1463 memberMapping.Elements[0].IsNullable =
false;
1465 return memberMapping;
1468 internal static XmlReflectionMember FindSpecifiedMember(
string memberName, XmlReflectionMember[] reflectionMembers)
1470 for (
int i = 0; i < reflectionMembers.Length; i++)
1472 if (
string.Compare(reflectionMembers[i].MemberName, memberName +
"Specified",
StringComparison.Ordinal) == 0)
1474 return reflectionMembers[i];
1480 private MemberMapping ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a,
string ns, RecursionLimiter limiter)
1482 MemberMapping memberMapping =
new MemberMapping();
1483 memberMapping.Name = model.Name;
1484 memberMapping.CheckShouldPersist = model.CheckShouldPersist;
1485 memberMapping.CheckSpecified = model.CheckSpecified;
1486 memberMapping.MemberInfo = model.MemberInfo;
1487 memberMapping.CheckSpecifiedMemberInfo = model.CheckSpecifiedMemberInfo;
1488 memberMapping.CheckShouldPersistMethodInfo = model.CheckShouldPersistMethodInfo;
1489 memberMapping.ReadOnly = model.ReadOnly;
1490 Type choiceIdentifierType =
null;
1491 if (a.XmlChoiceIdentifier !=
null)
1493 choiceIdentifierType = GetChoiceIdentifierType(a.XmlChoiceIdentifier, parent, model.FieldTypeDesc.IsArrayLike, model.Name);
1495 ImportAccessorMapping(memberMapping, model, a, ns, choiceIdentifierType, rpc:
false, openModel:
false, limiter);
1496 return memberMapping;
1499 private Type CheckChoiceIdentifierType(Type type,
bool isArrayLike,
string identifierName,
string memberName)
1505 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentifierType", identifierName, memberName, type.GetElementType().FullName));
1507 type = type.GetElementType();
1509 else if (isArrayLike)
1511 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentifierArrayType", identifierName, memberName, type.FullName));
1515 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentifierTypeEnum", identifierName));
1520 private Type GetChoiceIdentifierType(XmlChoiceIdentifierAttribute choice, XmlReflectionMember[] xmlReflectionMembers,
bool isArrayLike,
string accessorName)
1522 for (
int i = 0; i < xmlReflectionMembers.Length; i++)
1524 if (choice.MemberName == xmlReflectionMembers[i].MemberName)
1526 return CheckChoiceIdentifierType(xmlReflectionMembers[i].MemberType, isArrayLike, choice.MemberName, accessorName);
1529 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentiferMemberMissing", choice.MemberName, accessorName));
1532 private Type GetChoiceIdentifierType(XmlChoiceIdentifierAttribute choice, StructModel structModel,
bool isArrayLike,
string accessorName)
1535 if (array ==
null || array.Length == 0)
1538 if (property ==
null)
1540 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentiferMemberMissing", choice.MemberName, accessorName));
1547 else if (array.Length > 1)
1549 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentiferAmbiguous", choice.MemberName));
1551 FieldModel fieldModel = structModel.GetFieldModel(array[0]);
1552 if (fieldModel ==
null)
1554 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentiferMemberMissing", choice.MemberName, accessorName));
1556 choice.MemberInfo = fieldModel.MemberInfo;
1557 Type fieldType = fieldModel.FieldType;
1558 return CheckChoiceIdentifierType(fieldType, isArrayLike, choice.MemberName, accessorName);
1561 private void CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType,
string arrayElementNs, RecursionLimiter limiter)
1563 NameTable nameTable =
new NameTable();
1565 while (attributes !=
null && num < attributes.Count)
1567 XmlArrayItemAttribute xmlArrayItemAttribute = attributes[num];
1568 if (xmlArrayItemAttribute.NestingLevel == arrayNestingLevel)
1570 Type type = (xmlArrayItemAttribute.Type !=
null) ? xmlArrayItemAttribute.Type : arrayElementType;
1571 TypeDesc typeDesc = typeScope.GetTypeDesc(type);
1572 ElementAccessor elementAccessor =
new ElementAccessor();
1573 elementAccessor.Namespace = ((xmlArrayItemAttribute.Namespace ==
null) ? arrayElementNs : xmlArrayItemAttribute.Namespace);
1574 elementAccessor.Mapping =
ImportTypeMapping(modelScope.GetTypeModel(type), elementAccessor.Namespace, ImportContext.Element, xmlArrayItemAttribute.DataType,
null, limiter);
1575 elementAccessor.Name = ((xmlArrayItemAttribute.ElementName.Length == 0) ? elementAccessor.Mapping.DefaultElementName : XmlConvert.EncodeLocalName(xmlArrayItemAttribute.ElementName));
1576 elementAccessor.IsNullable = (xmlArrayItemAttribute.IsNullableSpecified ? xmlArrayItemAttribute.IsNullable : (typeDesc.IsNullable || typeDesc.IsOptionalValue));
1577 elementAccessor.Form = ((xmlArrayItemAttribute.Form ==
XmlSchemaForm.None) ?
XmlSchemaForm.Qualified : xmlArrayItemAttribute.Form);
1578 CheckForm(elementAccessor.Form, arrayElementNs != elementAccessor.Namespace);
1579 CheckNullable(elementAccessor.IsNullable, typeDesc, elementAccessor.Mapping);
1580 AddUniqueAccessor(nameTable, elementAccessor);
1584 arrayMapping.Elements = (ElementAccessor[])nameTable.ToArray(typeof(ElementAccessor));
1587 private void ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a,
string ns, Type choiceIdentifierType,
bool rpc,
bool openModel, RecursionLimiter limiter)
1590 int num = arrayNestingLevel;
1592 XmlArrayItemAttributes xmlArrayItemAttributes = savedArrayItemAttributes;
1593 string text = savedArrayNamespace;
1594 arrayNestingLevel = 0;
1595 savedArrayItemAttributes =
null;
1596 savedArrayNamespace =
null;
1597 Type fieldType = model.FieldType;
1598 string name = model.Name;
1600 NameTable nameTable =
new NameTable();
1601 accessor.TypeDesc = typeScope.GetTypeDesc(fieldType);
1602 XmlAttributeFlags xmlFlags = a.XmlFlags;
1603 accessor.Ignore = a.XmlIgnore;
1606 CheckTopLevelAttributes(a, name);
1610 CheckAmbiguousChoice(a, fieldType, name);
1612 XmlAttributeFlags xmlAttributeFlags = (XmlAttributeFlags)1300;
1613 XmlAttributeFlags xmlAttributeFlags2 = (XmlAttributeFlags)544;
1614 XmlAttributeFlags xmlAttributeFlags3 = (XmlAttributeFlags)10;
1615 if ((xmlFlags & xmlAttributeFlags3) != 0 && fieldType == typeof(
byte[]))
1617 accessor.TypeDesc = typeScope.GetArrayTypeDesc(fieldType);
1619 if (a.XmlChoiceIdentifier !=
null)
1621 accessor.ChoiceIdentifier =
new ChoiceIdentifierAccessor();
1622 accessor.ChoiceIdentifier.MemberName = a.XmlChoiceIdentifier.MemberName;
1623 accessor.ChoiceIdentifier.MemberInfo = a.XmlChoiceIdentifier.MemberInfo;
1624 accessor.ChoiceIdentifier.Mapping =
ImportTypeMapping(modelScope.GetTypeModel(choiceIdentifierType), ns, ImportContext.Element,
string.Empty,
null, limiter);
1625 CheckChoiceIdentifierMapping((EnumMapping)accessor.ChoiceIdentifier.Mapping);
1627 if (accessor.TypeDesc.IsArrayLike)
1629 Type arrayElementType = TypeScope.GetArrayElementType(fieldType, model.FieldTypeDesc.FullName +
"." + model.Name);
1630 if ((xmlFlags & xmlAttributeFlags2) != 0)
1632 if ((xmlFlags & xmlAttributeFlags2) != xmlFlags)
1634 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttributesArrayAttribute"));
1636 if (a.XmlAttribute !=
null && !accessor.TypeDesc.ArrayElementTypeDesc.IsPrimitive && !accessor.TypeDesc.ArrayElementTypeDesc.IsEnum)
1638 if (accessor.TypeDesc.ArrayElementTypeDesc.Kind == TypeKind.Serializable)
1640 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttrOrTextInterface", name, accessor.TypeDesc.ArrayElementTypeDesc.FullName, typeof(IXmlSerializable).Name));
1642 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttrOrText", name, accessor.TypeDesc.ArrayElementTypeDesc.FullName));
1644 bool flag = a.XmlAttribute !=
null && (accessor.TypeDesc.ArrayElementTypeDesc.IsPrimitive || accessor.TypeDesc.ArrayElementTypeDesc.IsEnum);
1645 if (a.XmlAnyAttribute !=
null)
1647 a.XmlAttribute =
new XmlAttributeAttribute();
1649 AttributeAccessor attributeAccessor =
new AttributeAccessor();
1650 Type type = (a.XmlAttribute.Type ==
null) ? arrayElementType : a.XmlAttribute.Type;
1651 TypeDesc typeDesc = typeScope.GetTypeDesc(type);
1652 attributeAccessor.Name = Accessor.EscapeQName((a.XmlAttribute.AttributeName.Length == 0) ? name : a.XmlAttribute.AttributeName);
1653 attributeAccessor.Namespace = ((a.XmlAttribute.Namespace ==
null) ? ns : a.XmlAttribute.Namespace);
1654 attributeAccessor.Form = a.XmlAttribute.Form;
1655 if (attributeAccessor.Form ==
XmlSchemaForm.None && ns != attributeAccessor.Namespace)
1659 attributeAccessor.CheckSpecial();
1660 CheckForm(attributeAccessor.Form, ns != attributeAccessor.Namespace);
1661 attributeAccessor.Mapping =
ImportTypeMapping(modelScope.GetTypeModel(type), ns, ImportContext.Attribute, a.XmlAttribute.DataType,
null, flag, openModel:
false, limiter);
1662 attributeAccessor.IsList = flag;
1663 attributeAccessor.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1664 attributeAccessor.Any = (a.XmlAnyAttribute !=
null);
1665 if (attributeAccessor.Form ==
XmlSchemaForm.Qualified && attributeAccessor.Namespace != ns)
1667 if (xsdAttributes ==
null)
1669 xsdAttributes =
new NameTable();
1671 attributeAccessor = (AttributeAccessor)ReconcileAccessor(attributeAccessor, xsdAttributes);
1673 accessor.Attribute = attributeAccessor;
1675 else if ((xmlFlags & xmlAttributeFlags) != 0)
1677 if ((xmlFlags & xmlAttributeFlags) != xmlFlags)
1679 throw new InvalidOperationException(Res.GetString(
"XmlIllegalElementsArrayAttribute"));
1681 if (a.XmlText !=
null)
1683 TextAccessor textAccessor =
new TextAccessor();
1684 Type type2 = (a.XmlText.Type ==
null) ? arrayElementType : a.XmlText.Type;
1685 TypeDesc typeDesc2 = typeScope.GetTypeDesc(type2);
1686 textAccessor.Name = name;
1687 textAccessor.Mapping =
ImportTypeMapping(modelScope.GetTypeModel(type2), ns, ImportContext.Text, a.XmlText.DataType,
null, repeats:
true, openModel:
false, limiter);
1688 if (!(textAccessor.Mapping is SpecialMapping) && typeDesc2 != typeScope.GetTypeDesc(typeof(
string)))
1690 throw new InvalidOperationException(Res.GetString(
"XmlIllegalArrayTextAttribute", name));
1692 accessor.Text = textAccessor;
1694 if (a.XmlText ==
null && a.XmlElements.Count == 0 && a.XmlAnyElements.Count == 0)
1696 a.XmlElements.Add(CreateElementAttribute(accessor.TypeDesc));
1698 for (
int i = 0; i < a.XmlElements.Count; i++)
1700 XmlElementAttribute xmlElementAttribute = a.XmlElements[i];
1701 Type type3 = (xmlElementAttribute.Type ==
null) ? arrayElementType : xmlElementAttribute.Type;
1702 TypeDesc typeDesc3 = typeScope.GetTypeDesc(type3);
1703 TypeModel typeModel = modelScope.GetTypeModel(type3);
1704 ElementAccessor elementAccessor =
new ElementAccessor();
1705 elementAccessor.Namespace = (rpc ? null : ((xmlElementAttribute.Namespace ==
null) ? ns : xmlElementAttribute.Namespace));
1706 elementAccessor.Mapping =
ImportTypeMapping(typeModel, rpc ? ns : elementAccessor.Namespace, ImportContext.Element, xmlElementAttribute.DataType,
null, limiter);
1707 if (a.XmlElements.Count == 1)
1709 elementAccessor.Name = XmlConvert.EncodeLocalName((xmlElementAttribute.ElementName.Length == 0) ? name : xmlElementAttribute.ElementName);
1713 elementAccessor.Name = ((xmlElementAttribute.ElementName.Length == 0) ? elementAccessor.Mapping.DefaultElementName : XmlConvert.EncodeLocalName(xmlElementAttribute.ElementName));
1715 elementAccessor.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1716 if (xmlElementAttribute.IsNullableSpecified && !xmlElementAttribute.IsNullable && typeModel.TypeDesc.IsOptionalValue)
1718 throw new InvalidOperationException(Res.GetString(
"XmlInvalidNotNullable", typeModel.TypeDesc.BaseTypeDesc.FullName,
"XmlElement"));
1720 elementAccessor.IsNullable = (xmlElementAttribute.IsNullableSpecified ? xmlElementAttribute.IsNullable : typeModel.TypeDesc.IsOptionalValue);
1721 elementAccessor.Form = (rpc ?
XmlSchemaForm.Unqualified : ((xmlElementAttribute.Form ==
XmlSchemaForm.None) ? xmlSchemaForm : xmlElementAttribute.Form));
1722 CheckNullable(elementAccessor.IsNullable, typeDesc3, elementAccessor.Mapping);
1725 CheckForm(elementAccessor.Form, ns != elementAccessor.Namespace);
1726 elementAccessor = ReconcileLocalAccessor(elementAccessor, ns);
1728 if (xmlElementAttribute.Order != -1)
1730 if (xmlElementAttribute.Order != num2 && num2 != -1)
1732 throw new InvalidOperationException(Res.GetString(
"XmlSequenceMatch",
"Order"));
1734 num2 = xmlElementAttribute.Order;
1736 AddUniqueAccessor(nameTable, elementAccessor);
1737 arrayList.
Add(elementAccessor);
1739 NameTable nameTable2 =
new NameTable();
1740 for (
int j = 0; j < a.XmlAnyElements.Count; j++)
1742 XmlAnyElementAttribute xmlAnyElementAttribute = a.XmlAnyElements[j];
1743 Type type4 = typeof(IXmlSerializable).IsAssignableFrom(arrayElementType) ? arrayElementType : (typeof(XmlNode).IsAssignableFrom(arrayElementType) ? arrayElementType : typeof(XmlElement));
1744 if (!arrayElementType.IsAssignableFrom(type4))
1746 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAnyElement", arrayElementType.FullName));
1748 string name2 = (xmlAnyElementAttribute.Name.Length == 0) ? xmlAnyElementAttribute.Name : XmlConvert.EncodeLocalName(xmlAnyElementAttribute.Name);
1749 string text2 = xmlAnyElementAttribute.NamespaceSpecified ? xmlAnyElementAttribute.Namespace :
null;
1750 if (nameTable2[name2, text2] !=
null)
1754 nameTable2[name2, text2] = xmlAnyElementAttribute;
1755 if (nameTable[name2, (text2 ==
null) ? ns : text2] !=
null)
1757 throw new InvalidOperationException(Res.GetString(
"XmlAnyElementDuplicate", name, xmlAnyElementAttribute.Name, (xmlAnyElementAttribute.Namespace ==
null) ?
"null" : xmlAnyElementAttribute.Namespace));
1759 ElementAccessor elementAccessor2 =
new ElementAccessor();
1760 elementAccessor2.Name = name2;
1761 elementAccessor2.Namespace = ((text2 ==
null) ? ns : text2);
1762 elementAccessor2.Any =
true;
1763 elementAccessor2.AnyNamespaces = text2;
1764 TypeDesc typeDesc4 = typeScope.GetTypeDesc(type4);
1765 TypeModel typeModel2 = modelScope.GetTypeModel(type4);
1766 if (elementAccessor2.Name.Length > 0)
1768 typeModel2.TypeDesc.IsMixed =
true;
1770 elementAccessor2.Mapping =
ImportTypeMapping(typeModel2, elementAccessor2.Namespace, ImportContext.Element,
string.Empty,
null, limiter);
1771 elementAccessor2.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1772 elementAccessor2.IsNullable =
false;
1773 elementAccessor2.Form = xmlSchemaForm;
1774 CheckNullable(elementAccessor2.IsNullable, typeDesc4, elementAccessor2.Mapping);
1777 CheckForm(elementAccessor2.Form, ns != elementAccessor2.Namespace);
1778 elementAccessor2 = ReconcileLocalAccessor(elementAccessor2, ns);
1780 nameTable.Add(elementAccessor2.Name, elementAccessor2.Namespace, elementAccessor2);
1781 arrayList.
Add(elementAccessor2);
1782 if (xmlAnyElementAttribute.Order != -1)
1784 if (xmlAnyElementAttribute.Order != num2 && num2 != -1)
1786 throw new InvalidOperationException(Res.GetString(
"XmlSequenceMatch",
"Order"));
1788 num2 = xmlAnyElementAttribute.Order;
1794 if ((xmlFlags & xmlAttributeFlags3) != 0 && (xmlFlags & xmlAttributeFlags3) != xmlFlags)
1796 throw new InvalidOperationException(Res.GetString(
"XmlIllegalArrayArrayAttribute"));
1798 TypeDesc typeDesc5 = typeScope.GetTypeDesc(arrayElementType);
1799 if (a.XmlArray ==
null)
1801 a.XmlArray = CreateArrayAttribute(accessor.TypeDesc);
1803 if (CountAtLevel(a.XmlArrayItems, arrayNestingLevel) == 0)
1805 a.XmlArrayItems.Add(CreateArrayItemAttribute(typeDesc5, arrayNestingLevel));
1807 ElementAccessor elementAccessor3 =
new ElementAccessor();
1808 elementAccessor3.Name = XmlConvert.EncodeLocalName((a.XmlArray.ElementName.Length == 0) ? name : a.XmlArray.ElementName);
1809 elementAccessor3.Namespace = (rpc ? null : ((a.XmlArray.Namespace ==
null) ? ns : a.XmlArray.Namespace));
1810 savedArrayItemAttributes = a.XmlArrayItems;
1811 savedArrayNamespace = elementAccessor3.Namespace;
1812 ArrayMapping arrayMapping = (ArrayMapping)(elementAccessor3.Mapping = ImportArrayLikeMapping(modelScope.GetArrayModel(fieldType), ns, limiter));
1813 elementAccessor3.IsNullable = a.XmlArray.IsNullable;
1814 elementAccessor3.Form = (rpc ?
XmlSchemaForm.Unqualified : ((a.XmlArray.Form ==
XmlSchemaForm.None) ? xmlSchemaForm : a.XmlArray.Form));
1815 num2 = a.XmlArray.Order;
1816 CheckNullable(elementAccessor3.IsNullable, accessor.TypeDesc, elementAccessor3.Mapping);
1819 CheckForm(elementAccessor3.Form, ns != elementAccessor3.Namespace);
1820 elementAccessor3 = ReconcileLocalAccessor(elementAccessor3, ns);
1822 savedArrayItemAttributes =
null;
1823 savedArrayNamespace =
null;
1824 AddUniqueAccessor(nameTable, elementAccessor3);
1825 arrayList.
Add(elementAccessor3);
1828 else if (!accessor.TypeDesc.IsVoid)
1830 XmlAttributeFlags xmlAttributeFlags4 = (XmlAttributeFlags)3380;
1831 if ((xmlFlags & xmlAttributeFlags4) != xmlFlags)
1833 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttribute"));
1835 if (accessor.TypeDesc.IsPrimitive || accessor.TypeDesc.IsEnum)
1837 if (a.XmlAnyElements.Count > 0)
1839 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAnyElement", accessor.TypeDesc.FullName));
1841 if (a.XmlAttribute !=
null)
1843 if (a.XmlElements.Count > 0)
1845 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttribute"));
1847 if (a.XmlAttribute.Type !=
null)
1849 throw new InvalidOperationException(Res.GetString(
"XmlIllegalType",
"XmlAttribute"));
1851 AttributeAccessor attributeAccessor2 =
new AttributeAccessor();
1852 attributeAccessor2.Name = Accessor.EscapeQName((a.XmlAttribute.AttributeName.Length == 0) ? name : a.XmlAttribute.AttributeName);
1853 attributeAccessor2.Namespace = ((a.XmlAttribute.Namespace ==
null) ? ns : a.XmlAttribute.Namespace);
1854 attributeAccessor2.Form = a.XmlAttribute.Form;
1855 if (attributeAccessor2.Form ==
XmlSchemaForm.None && ns != attributeAccessor2.Namespace)
1859 attributeAccessor2.CheckSpecial();
1860 CheckForm(attributeAccessor2.Form, ns != attributeAccessor2.Namespace);
1861 attributeAccessor2.Mapping =
ImportTypeMapping(modelScope.GetTypeModel(fieldType), ns, ImportContext.Attribute, a.XmlAttribute.DataType,
null, limiter);
1862 attributeAccessor2.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1863 attributeAccessor2.Any = (a.XmlAnyAttribute !=
null);
1864 if (attributeAccessor2.Form ==
XmlSchemaForm.Qualified && attributeAccessor2.Namespace != ns)
1866 if (xsdAttributes ==
null)
1868 xsdAttributes =
new NameTable();
1870 attributeAccessor2 = (AttributeAccessor)ReconcileAccessor(attributeAccessor2, xsdAttributes);
1872 accessor.Attribute = attributeAccessor2;
1876 if (a.XmlText !=
null)
1878 if (a.XmlText.Type !=
null && a.XmlText.Type != fieldType)
1880 throw new InvalidOperationException(Res.GetString(
"XmlIllegalType",
"XmlText"));
1882 TextAccessor textAccessor2 =
new TextAccessor();
1883 textAccessor2.Name = name;
1884 textAccessor2.Mapping =
ImportTypeMapping(modelScope.GetTypeModel(fieldType), ns, ImportContext.Text, a.XmlText.DataType,
null, limiter);
1885 accessor.Text = textAccessor2;
1887 else if (a.XmlElements.Count == 0)
1889 a.XmlElements.Add(CreateElementAttribute(accessor.TypeDesc));
1891 for (
int k = 0; k < a.XmlElements.Count; k++)
1893 XmlElementAttribute xmlElementAttribute2 = a.XmlElements[k];
1894 if (xmlElementAttribute2.Type !=
null && typeScope.GetTypeDesc(xmlElementAttribute2.Type) != accessor.TypeDesc)
1896 throw new InvalidOperationException(Res.GetString(
"XmlIllegalType",
"XmlElement"));
1898 ElementAccessor elementAccessor4 =
new ElementAccessor();
1899 elementAccessor4.Name = XmlConvert.EncodeLocalName((xmlElementAttribute2.ElementName.Length == 0) ? name : xmlElementAttribute2.ElementName);
1900 elementAccessor4.Namespace = (rpc ? null : ((xmlElementAttribute2.Namespace ==
null) ? ns : xmlElementAttribute2.Namespace));
1901 TypeModel typeModel3 = modelScope.GetTypeModel(fieldType);
1902 elementAccessor4.Mapping =
ImportTypeMapping(typeModel3, rpc ? ns : elementAccessor4.Namespace, ImportContext.Element, xmlElementAttribute2.DataType,
null, limiter);
1903 if (elementAccessor4.Mapping.TypeDesc.Kind == TypeKind.Node)
1905 elementAccessor4.Any =
true;
1907 elementAccessor4.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1908 if (xmlElementAttribute2.IsNullableSpecified && !xmlElementAttribute2.IsNullable && typeModel3.TypeDesc.IsOptionalValue)
1910 throw new InvalidOperationException(Res.GetString(
"XmlInvalidNotNullable", typeModel3.TypeDesc.BaseTypeDesc.FullName,
"XmlElement"));
1912 elementAccessor4.IsNullable = (xmlElementAttribute2.IsNullableSpecified ? xmlElementAttribute2.IsNullable : typeModel3.TypeDesc.IsOptionalValue);
1913 elementAccessor4.Form = (rpc ?
XmlSchemaForm.Unqualified : ((xmlElementAttribute2.Form ==
XmlSchemaForm.None) ? xmlSchemaForm : xmlElementAttribute2.Form));
1914 CheckNullable(elementAccessor4.IsNullable, accessor.TypeDesc, elementAccessor4.Mapping);
1917 CheckForm(elementAccessor4.Form, ns != elementAccessor4.Namespace);
1918 elementAccessor4 = ReconcileLocalAccessor(elementAccessor4, ns);
1920 if (xmlElementAttribute2.Order != -1)
1922 if (xmlElementAttribute2.Order != num2 && num2 != -1)
1924 throw new InvalidOperationException(Res.GetString(
"XmlSequenceMatch",
"Order"));
1926 num2 = xmlElementAttribute2.Order;
1928 AddUniqueAccessor(nameTable, elementAccessor4);
1929 arrayList.
Add(elementAccessor4);
1935 if (xmlFlags != XmlAttributeFlags.XmlnsDeclarations)
1937 throw new InvalidOperationException(Res.GetString(
"XmlSoleXmlnsAttribute"));
1939 if (fieldType != typeof(XmlSerializerNamespaces))
1941 throw new InvalidOperationException(Res.GetString(
"XmlXmlnsInvalidType", name, fieldType.FullName, typeof(XmlSerializerNamespaces).FullName));
1943 accessor.Xmlns =
new XmlnsAccessor();
1944 accessor.Ignore =
true;
1948 if (a.XmlAttribute !=
null || a.XmlText !=
null)
1950 if (accessor.TypeDesc.Kind == TypeKind.Serializable)
1952 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttrOrTextInterface", name, accessor.TypeDesc.FullName, typeof(IXmlSerializable).Name));
1954 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAttrOrText", name, accessor.TypeDesc));
1956 if (a.XmlElements.Count == 0 && a.XmlAnyElements.Count == 0)
1958 a.XmlElements.Add(CreateElementAttribute(accessor.TypeDesc));
1960 for (
int l = 0; l < a.XmlElements.Count; l++)
1962 XmlElementAttribute xmlElementAttribute3 = a.XmlElements[l];
1963 Type type5 = (xmlElementAttribute3.Type ==
null) ? fieldType : xmlElementAttribute3.Type;
1964 TypeDesc typeDesc6 = typeScope.GetTypeDesc(type5);
1965 ElementAccessor elementAccessor5 =
new ElementAccessor();
1966 TypeModel typeModel4 = modelScope.GetTypeModel(type5);
1967 elementAccessor5.Namespace = (rpc ? null : ((xmlElementAttribute3.Namespace ==
null) ? ns : xmlElementAttribute3.Namespace));
1968 elementAccessor5.Mapping =
ImportTypeMapping(typeModel4, rpc ? ns : elementAccessor5.Namespace, ImportContext.Element, xmlElementAttribute3.DataType,
null, repeats:
false, openModel, limiter);
1969 if (a.XmlElements.Count == 1)
1971 elementAccessor5.Name = XmlConvert.EncodeLocalName((xmlElementAttribute3.ElementName.Length == 0) ? name : xmlElementAttribute3.ElementName);
1975 elementAccessor5.Name = ((xmlElementAttribute3.ElementName.Length == 0) ? elementAccessor5.Mapping.DefaultElementName : XmlConvert.EncodeLocalName(xmlElementAttribute3.ElementName));
1977 elementAccessor5.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1978 if (xmlElementAttribute3.IsNullableSpecified && !xmlElementAttribute3.IsNullable && typeModel4.TypeDesc.IsOptionalValue)
1980 throw new InvalidOperationException(Res.GetString(
"XmlInvalidNotNullable", typeModel4.TypeDesc.BaseTypeDesc.FullName,
"XmlElement"));
1982 elementAccessor5.IsNullable = (xmlElementAttribute3.IsNullableSpecified ? xmlElementAttribute3.IsNullable : typeModel4.TypeDesc.IsOptionalValue);
1983 elementAccessor5.Form = (rpc ?
XmlSchemaForm.Unqualified : ((xmlElementAttribute3.Form ==
XmlSchemaForm.None) ? xmlSchemaForm : xmlElementAttribute3.Form));
1984 CheckNullable(elementAccessor5.IsNullable, typeDesc6, elementAccessor5.Mapping);
1987 CheckForm(elementAccessor5.Form, ns != elementAccessor5.Namespace);
1988 elementAccessor5 = ReconcileLocalAccessor(elementAccessor5, ns);
1990 if (xmlElementAttribute3.Order != -1)
1992 if (xmlElementAttribute3.Order != num2 && num2 != -1)
1994 throw new InvalidOperationException(Res.GetString(
"XmlSequenceMatch",
"Order"));
1996 num2 = xmlElementAttribute3.Order;
1998 AddUniqueAccessor(nameTable, elementAccessor5);
1999 arrayList.
Add(elementAccessor5);
2001 NameTable nameTable3 =
new NameTable();
2002 for (
int m = 0; m < a.XmlAnyElements.Count; m++)
2004 XmlAnyElementAttribute xmlAnyElementAttribute2 = a.XmlAnyElements[m];
2005 Type type6 = typeof(IXmlSerializable).IsAssignableFrom(fieldType) ? fieldType : (typeof(XmlNode).IsAssignableFrom(fieldType) ? fieldType : typeof(XmlElement));
2006 if (!fieldType.IsAssignableFrom(type6))
2008 throw new InvalidOperationException(Res.GetString(
"XmlIllegalAnyElement", fieldType.FullName));
2010 string name3 = (xmlAnyElementAttribute2.Name.Length == 0) ? xmlAnyElementAttribute2.Name : XmlConvert.EncodeLocalName(xmlAnyElementAttribute2.Name);
2011 string text3 = xmlAnyElementAttribute2.NamespaceSpecified ? xmlAnyElementAttribute2.Namespace :
null;
2012 if (nameTable3[name3, text3] !=
null)
2016 nameTable3[name3, text3] = xmlAnyElementAttribute2;
2017 if (nameTable[name3, (text3 ==
null) ? ns : text3] !=
null)
2019 throw new InvalidOperationException(Res.GetString(
"XmlAnyElementDuplicate", name, xmlAnyElementAttribute2.Name, (xmlAnyElementAttribute2.Namespace ==
null) ?
"null" : xmlAnyElementAttribute2.Namespace));
2021 ElementAccessor elementAccessor6 =
new ElementAccessor();
2022 elementAccessor6.Name = name3;
2023 elementAccessor6.Namespace = ((text3 ==
null) ? ns : text3);
2024 elementAccessor6.Any =
true;
2025 elementAccessor6.AnyNamespaces = text3;
2026 TypeDesc typeDesc7 = typeScope.GetTypeDesc(type6);
2027 TypeModel typeModel5 = modelScope.GetTypeModel(type6);
2028 if (elementAccessor6.Name.Length > 0)
2030 typeModel5.TypeDesc.IsMixed =
true;
2032 elementAccessor6.Mapping =
ImportTypeMapping(typeModel5, elementAccessor6.Namespace, ImportContext.Element,
string.Empty,
null, repeats:
false, openModel, limiter);
2033 elementAccessor6.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
2034 elementAccessor6.IsNullable =
false;
2035 elementAccessor6.Form = xmlSchemaForm;
2036 CheckNullable(elementAccessor6.IsNullable, typeDesc7, elementAccessor6.Mapping);
2039 CheckForm(elementAccessor6.Form, ns != elementAccessor6.Namespace);
2040 elementAccessor6 = ReconcileLocalAccessor(elementAccessor6, ns);
2042 if (xmlAnyElementAttribute2.Order != -1)
2044 if (xmlAnyElementAttribute2.Order != num2 && num2 != -1)
2046 throw new InvalidOperationException(Res.GetString(
"XmlSequenceMatch",
"Order"));
2048 num2 = xmlAnyElementAttribute2.Order;
2050 nameTable.Add(elementAccessor6.Name, elementAccessor6.Namespace, elementAccessor6);
2051 arrayList.
Add(elementAccessor6);
2055 accessor.Elements = (ElementAccessor[])arrayList.
ToArray(typeof(ElementAccessor));
2056 accessor.SequenceId = num2;
2059 if (accessor.TypeDesc.IsArrayLike && accessor.Elements.Length != 0 && !(accessor.Elements[0].Mapping is ArrayMapping))
2061 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitArrayElement", accessor.Elements[0].Name));
2063 if (accessor.Xmlns !=
null)
2065 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitXmlns", accessor.Name));
2068 if (accessor.ChoiceIdentifier !=
null)
2070 accessor.ChoiceIdentifier.MemberIds =
new string[accessor.Elements.Length];
2071 for (
int n = 0; n < accessor.Elements.Length; n++)
2074 ElementAccessor elementAccessor7 = accessor.Elements[n];
2075 EnumMapping enumMapping = (EnumMapping)accessor.ChoiceIdentifier.Mapping;
2076 for (
int num3 = 0; num3 < enumMapping.Constants.Length; num3++)
2078 string xmlName = enumMapping.Constants[num3].XmlName;
2079 if (elementAccessor7.Any && elementAccessor7.Name.Length == 0)
2081 string b = (elementAccessor7.AnyNamespaces ==
null) ?
"##any" : elementAccessor7.AnyNamespaces;
2082 if (xmlName.Substring(0, xmlName.Length - 1) == b)
2084 accessor.ChoiceIdentifier.MemberIds[n] = enumMapping.Constants[num3].Name;
2090 int num4 = xmlName.LastIndexOf(
':');
2091 string text4 = (num4 < 0) ? enumMapping.Namespace : xmlName.Substring(0, num4);
2092 string b2 = (num4 < 0) ? xmlName : xmlName.Substring(num4 + 1);
2093 if (elementAccessor7.Name == b2 && ((elementAccessor7.Form ==
XmlSchemaForm.Unqualified &&
string.IsNullOrEmpty(text4)) || elementAccessor7.Namespace == text4))
2095 accessor.ChoiceIdentifier.MemberIds[n] = enumMapping.Constants[num3].Name;
2102 if (elementAccessor7.Any && elementAccessor7.Name.Length == 0)
2104 throw new InvalidOperationException(Res.GetString(
"XmlChoiceMissingAnyValue", accessor.ChoiceIdentifier.Mapping.TypeDesc.FullName));
2106 string text5 = (elementAccessor7.Namespace !=
null && elementAccessor7.Namespace.Length > 0) ? (elementAccessor7.Namespace +
":" + elementAccessor7.Name) : elementAccessor7.Name;
2107 throw new InvalidOperationException(Res.GetString(
"XmlChoiceMissingValue", accessor.ChoiceIdentifier.Mapping.TypeDesc.FullName, text5, elementAccessor7.Name, elementAccessor7.Namespace));
2111 arrayNestingLevel = num;
2112 savedArrayItemAttributes = xmlArrayItemAttributes;
2113 savedArrayNamespace = text;
2116 private void CheckTopLevelAttributes(XmlAttributes a,
string accessorName)
2118 XmlAttributeFlags xmlFlags = a.XmlFlags;
2119 if ((xmlFlags & (XmlAttributeFlags)544) != 0)
2121 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitAttributeAttributes"));
2123 if ((xmlFlags & (XmlAttributeFlags)1284) != 0)
2125 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitAttributes"));
2127 if (a.XmlElements !=
null && a.XmlElements.Count > 0)
2129 if (a.XmlElements.Count > 1)
2131 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitElements"));
2133 XmlElementAttribute xmlElementAttribute = a.XmlElements[0];
2134 if (xmlElementAttribute.Namespace !=
null)
2136 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitElementNamespace",
"Namespace", xmlElementAttribute.Namespace));
2138 if (xmlElementAttribute.IsNullable)
2140 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitElementNullable",
"IsNullable",
"true"));
2143 if (a.XmlArray !=
null && a.XmlArray.Namespace !=
null)
2145 throw new InvalidOperationException(Res.GetString(
"XmlRpcLitElementNamespace",
"Namespace", a.XmlArray.Namespace));
2149 private void CheckAmbiguousChoice(XmlAttributes a, Type accessorType,
string accessorName)
2152 XmlElementAttributes xmlElements = a.XmlElements;
2153 if (xmlElements !=
null && xmlElements.Count >= 2 && a.XmlChoiceIdentifier ==
null)
2155 for (
int i = 0; i < xmlElements.Count; i++)
2157 Type key = (xmlElements[i].Type ==
null) ? accessorType : xmlElements[i].Type;
2160 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentiferMissing", typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
2162 hashtable.
Add(key,
false);
2165 if (hashtable.
Contains(typeof(XmlElement)) && a.XmlAnyElements.Count > 0)
2167 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdentiferMissing", typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
2169 XmlArrayItemAttributes xmlArrayItems = a.XmlArrayItems;
2170 if (xmlArrayItems ==
null || xmlArrayItems.Count < 2)
2174 NameTable nameTable =
new NameTable();
2176 XmlArrayItemAttribute xmlArrayItemAttribute;
2179 if (num < xmlArrayItems.Count)
2181 Type type = (xmlArrayItems[num].Type ==
null) ? accessorType : xmlArrayItems[num].Type;
2183 xmlArrayItemAttribute = (XmlArrayItemAttribute)nameTable[type.FullName, ns];
2184 if (xmlArrayItemAttribute !=
null)
2188 nameTable[type.FullName, ns] = xmlArrayItems[num];
2194 throw new InvalidOperationException(Res.GetString(
"XmlArrayItemAmbiguousTypes", accessorName, xmlArrayItemAttribute.ElementName, xmlArrayItems[num].ElementName, typeof(XmlElementAttribute).Name, typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
2197 private void CheckChoiceIdentifierMapping(EnumMapping choiceMapping)
2199 NameTable nameTable =
new NameTable();
2204 if (num < choiceMapping.Constants.Length)
2206 xmlName = choiceMapping.Constants[num].XmlName;
2207 int num2 = xmlName.LastIndexOf(
':');
2208 string name = (num2 < 0) ? xmlName : xmlName.Substring(num2 + 1);
2209 string ns = (num2 < 0) ?
"" : xmlName.Substring(0, num2);
2210 if (nameTable[name, ns] !=
null)
2214 nameTable.Add(name, ns, choiceMapping.Constants[num]);
2220 throw new InvalidOperationException(Res.GetString(
"XmlChoiceIdDuplicate", choiceMapping.TypeName, xmlName));
2223 private object GetDefaultValue(TypeDesc fieldTypeDesc, Type t, XmlAttributes a)
2225 if (a.XmlDefaultValue ==
null || a.XmlDefaultValue ==
DBNull.Value)
2229 if (fieldTypeDesc.Kind != TypeKind.Primitive && fieldTypeDesc.Kind != TypeKind.Enum)
2231 a.XmlDefaultValue =
null;
2232 return a.XmlDefaultValue;
2234 if (fieldTypeDesc.Kind == TypeKind.Enum)
2236 string text =
Enum.Format(t, a.XmlDefaultValue,
"G").Replace(
",",
" ");
2237 string b =
Enum.Format(t, a.XmlDefaultValue,
"D");
2240 throw new InvalidOperationException(Res.GetString(
"XmlInvalidDefaultValue", text, a.XmlDefaultValue.GetType().FullName));
2244 return a.XmlDefaultValue;
2247 private static XmlArrayItemAttribute CreateArrayItemAttribute(TypeDesc typeDesc,
int nestingLevel)
2249 XmlArrayItemAttribute xmlArrayItemAttribute =
new XmlArrayItemAttribute();
2250 xmlArrayItemAttribute.NestingLevel = nestingLevel;
2251 return xmlArrayItemAttribute;
2254 private static XmlArrayAttribute CreateArrayAttribute(TypeDesc typeDesc)
2256 return new XmlArrayAttribute();
2259 private static XmlElementAttribute CreateElementAttribute(TypeDesc typeDesc)
2261 XmlElementAttribute xmlElementAttribute =
new XmlElementAttribute();
2262 xmlElementAttribute.IsNullable = typeDesc.IsOptionalValue;
2263 return xmlElementAttribute;
2266 private static void AddUniqueAccessor(INameScope scope, Accessor accessor)
2268 Accessor accessor2 = (Accessor)scope[accessor.Name, accessor.Namespace];
2269 if (accessor2 !=
null)
2271 if (accessor is ElementAccessor)
2273 throw new InvalidOperationException(Res.GetString(
"XmlDuplicateElementName", accessor2.Name, accessor2.Namespace));
2275 throw new InvalidOperationException(Res.GetString(
"XmlDuplicateAttributeName", accessor2.Name, accessor2.Namespace));
2277 scope[accessor.Name, accessor.Namespace] = accessor;
2280 private static void AddUniqueAccessor(MemberMapping member, INameScope elements, INameScope attributes,
bool isSequence)
2282 if (member.Attribute !=
null)
2284 AddUniqueAccessor(attributes, member.Attribute);
2286 else if (!isSequence && member.Elements !=
null && member.Elements.Length != 0)
2288 for (
int i = 0; i < member.Elements.Length; i++)
2290 AddUniqueAccessor(elements, member.Elements[i]);
2295 private static void CheckForm(
XmlSchemaForm form,
bool isQualified)
2299 throw new InvalidOperationException(Res.GetString(
"XmlInvalidFormUnqualified"));
2303 private static void CheckNullable(
bool isNullable, TypeDesc typeDesc, TypeMapping mapping)
2305 if (mapping is NullableMapping || mapping is SerializableMapping || !isNullable || typeDesc.IsNullable)
2309 throw new InvalidOperationException(Res.GetString(
"XmlInvalidIsNullable", typeDesc.FullName));
2312 private static ElementAccessor CreateElementAccessor(TypeMapping mapping,
string ns)
2314 ElementAccessor elementAccessor =
new ElementAccessor();
2315 bool flag = mapping.TypeDesc.Kind == TypeKind.Node;
2316 if (!flag && mapping is SerializableMapping)
2318 flag = ((SerializableMapping)mapping).IsAny;
2322 elementAccessor.Any =
true;
2326 elementAccessor.Name = mapping.DefaultElementName;
2327 elementAccessor.Namespace = ns;
2329 elementAccessor.Mapping = mapping;
2330 return elementAccessor;
2333 internal static XmlTypeMapping GetTopLevelMapping(Type type,
string defaultNamespace)
2335 XmlAttributes xmlAttributes =
new XmlAttributes(type);
2336 TypeDesc typeDesc =
new TypeScope().GetTypeDesc(type);
2337 ElementAccessor elementAccessor =
new ElementAccessor();
2338 if (typeDesc.Kind == TypeKind.Node)
2340 elementAccessor.Any =
true;
2344 string @
namespace = (xmlAttributes.XmlRoot ==
null) ? defaultNamespace : xmlAttributes.XmlRoot.Namespace;
2345 string text =
string.Empty;
2346 if (xmlAttributes.XmlType !=
null)
2348 text = xmlAttributes.XmlType.TypeName;
2350 if (text.Length == 0)
2354 elementAccessor.Name = XmlConvert.EncodeLocalName(text);
2355 elementAccessor.Namespace = @
namespace;
2357 XmlTypeMapping xmlTypeMapping =
new XmlTypeMapping(
null, elementAccessor);
2358 xmlTypeMapping.SetKeyInternal(XmlMapping.GenerateKey(type, xmlAttributes.XmlRoot, defaultNamespace));
2359 return xmlTypeMapping;
Obtains information about the attributes of a member and provides access to member metadata.
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Provides mappings between .NET Framework Web service methods and Web Services Description Language (W...
Allows the T:System.Xml.Serialization.XmlSerializer to recognize a type when it serializes or deseria...
XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, bool openModel, XmlMappingAccess access)
Generates internal type mappings for information from a Web service method.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
The base class for all simple types and complex types.
XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement)
Generates internal type mappings for information from a Web service method.
object [] GetCustomAttributes(Type attributeType, bool inherit)
Returns an array of custom attributes defined on this member, identified by type, or an empty array i...
virtual void Add(object key, object value)
Adds an element with the specified key and value into the T:System.Collections.Hashtable.
abstract Type DeclaringType
Gets the class that declares this member.
Discovers the attributes of a method and provides access to method metadata.
abstract string FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
XmlReflectionImporter(XmlAttributeOverrides attributeOverrides, string defaultNamespace)
Initializes a new instance of the T:System.Xml.Serialization.XmlReflectionImporter class using the sp...
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
int Add(XmlArrayItemAttribute attribute)
Adds an T:System.Xml.Serialization.XmlArrayItemAttribute to the collection.
void IncludeType(Type type)
Includes mappings for a type for later use when import methods are invoked.
string Message
Gets the text description corresponding to the validation event.
XmlSchemaForm
Indicates if attributes or elements need to be qualified with a namespace prefix.
Represents the base class for custom attributes.
XmlSeverityType
Represents the severity of the validation event.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
virtual Type ReturnType
Gets the return type of this method.
XmlTypeMapping ImportTypeMapping(Type type, XmlRootAttribute root, string defaultNamespace)
Generates a mapping to an XML Schema element for a .NET Framework type, using the specified type,...
XmlReflectionImporter(XmlAttributeOverrides attributeOverrides)
Initializes a new instance of the T:System.Xml.Serialization.XmlReflectionImporter class using the sp...
Describes the context in which a set of schema is bound to .NET Framework code entities.
static string EncodeLocalName(string name)
Converts the name to a valid XML local name.
ICollection Schemas()
Returns a collection of all the XML Schema definition language (XSD) schemas in the T:System....
XmlMappingAccess
Specifies whether a mapping is read, write, or both.
Provides mappings between code entities in .NET Framework Web service methods and the content of Web ...
XmlTypeMapping ImportTypeMapping(Type type)
Generates a mapping to an XML Schema element for a specified .NET Framework type.
Controls XML serialization of the attribute target as an XML root element.
XmlSeverityType Severity
Gets the severity of the validation event.
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
Supports mappings between .NET Framework types and XML Schema data types.
Represents a collection of key/value pairs that are organized based on the hash code of the key....
A database null (column) value.
XmlTypeMapping ImportTypeMapping(Type type, XmlRootAttribute root)
Generates a mapping to an XML Schema element for a .NET Framework type, using the specified type and ...
Represents type declarations: class types, interface types, array types, value types,...
XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, bool openModel)
Returns internal type mappings using information from a Web service method, and allows you to specify...
abstract string Name
Gets the name of the current member.
Generates mappings to XML schema element declarations, including literal XML Schema Definition (XSD) ...
Represents a collection of attribute objects that control how the T:System.Xml.Serialization....
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Discovers the attributes of a property and provides access to property metadata.
Attribute can be applied to an enumeration.
virtual bool Contains(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
XmlSchemaObjectTable SchemaTypes
Gets the post-schema-compilation value of all schema types in the schema.
void IncludeTypes(ICustomAttributeProvider provider)
Includes mappings for derived types for later use when import methods are invoked.
XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace)
Generates a mapping to an XML Schema element for a .NET Framework type, using the specified type and ...
An in-memory representation of an XML Schema, as specified in the World Wide Web Consortium (W3C) XML...
XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc)
Returns internal type mappings using information from a Web service method, and allows you to specify...
The exception that is thrown when a method call is invalid for the object's current state.
Allows you to override property, field, and class attributes when you use the T:System....
abstract MemberTypes MemberType
When overridden in a derived class, gets a T:System.Reflection.MemberTypes value indicating the type ...
Provides information about a specific culture (called a locale for unmanaged code development)....
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Contains a mapping of one type to another.
The exception that is thrown when a call is made to the M:System.Threading.Thread....
MemberTypes
Marks each type of member that is defined as a derived class of T:System.Reflection....
Returns detailed information related to the ValidationEventHandler.
XmlReflectionImporter()
Initializes a new instance of the T:System.Xml.Serialization.XmlReflectionImporter class.
Provides custom attributes for reflection objects that support them.
Represents a collection of T:System.Xml.Serialization.XmlArrayItemAttribute objects.
XmlReflectionImporter(string defaultNamespace)
Initializes a new instance of the T:System.Xml.Serialization.XmlReflectionImporter class using the sp...
virtual object [] ToArray()
Copies the elements of the T:System.Collections.ArrayList to a new T:System.Object array.
Encodes and decodes XML names, and provides methods for converting between common language runtime ty...
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Contains a cache of XML Schema definition language (XSD) schemas.
virtual void Sort()
Sorts the elements in the entire T:System.Collections.ArrayList.