mscorlib(4.0.0.0) API with additions
XmlReflectionImporter.cs
1 using System.Collections;
3 using System.Reflection;
4 using System.Threading;
5 using System.Xml.Schema;
6 
8 {
10  public class XmlReflectionImporter
11  {
12  private enum ImportContext
13  {
14  Text,
15  Attribute,
16  Element
17  }
18 
19  private TypeScope typeScope;
20 
21  private XmlAttributeOverrides attributeOverrides;
22 
23  private XmlAttributes defaultAttributes = new XmlAttributes();
24 
25  private NameTable types = new NameTable();
26 
27  private NameTable nullables = new NameTable();
28 
29  private NameTable elements = new NameTable();
30 
31  private NameTable xsdAttributes;
32 
33  private Hashtable specials;
34 
35  private Hashtable anonymous = new Hashtable();
36 
37  private NameTable serializables;
38 
39  private StructMapping root;
40 
41  private string defaultNs;
42 
43  private ModelScope modelScope;
44 
45  private int arrayNestingLevel;
46 
47  private XmlArrayItemAttributes savedArrayItemAttributes;
48 
49  private string savedArrayNamespace;
50 
51  private int choiceNum = 1;
52 
55  : this(null, null)
56  {
57  }
58 
61  public XmlReflectionImporter(string defaultNamespace)
62  : this(null, defaultNamespace)
63  {
64  }
65 
68  public XmlReflectionImporter(XmlAttributeOverrides attributeOverrides)
69  : this(attributeOverrides, null)
70  {
71  }
72 
76  public XmlReflectionImporter(XmlAttributeOverrides attributeOverrides, string defaultNamespace)
77  {
78  if (defaultNamespace == null)
79  {
80  defaultNamespace = string.Empty;
81  }
82  if (attributeOverrides == null)
83  {
84  attributeOverrides = new XmlAttributeOverrides();
85  }
86  this.attributeOverrides = attributeOverrides;
87  defaultNs = defaultNamespace;
88  typeScope = new TypeScope();
89  modelScope = new ModelScope(typeScope);
90  }
91 
94  public void IncludeTypes(ICustomAttributeProvider provider)
95  {
96  IncludeTypes(provider, new RecursionLimiter());
97  }
98 
99  private void IncludeTypes(ICustomAttributeProvider provider, RecursionLimiter limiter)
100  {
101  object[] customAttributes = provider.GetCustomAttributes(typeof(XmlIncludeAttribute), inherit: false);
102  for (int i = 0; i < customAttributes.Length; i++)
103  {
104  Type type = ((XmlIncludeAttribute)customAttributes[i]).Type;
105  IncludeType(type, limiter);
106  }
107  }
108 
111  public void IncludeType(Type type)
112  {
113  IncludeType(type, new RecursionLimiter());
114  }
115 
116  private void IncludeType(Type type, RecursionLimiter limiter)
117  {
118  int num = arrayNestingLevel;
119  XmlArrayItemAttributes xmlArrayItemAttributes = savedArrayItemAttributes;
120  string text = savedArrayNamespace;
121  arrayNestingLevel = 0;
122  savedArrayItemAttributes = null;
123  savedArrayNamespace = null;
124  TypeMapping typeMapping = ImportTypeMapping(modelScope.GetTypeModel(type), defaultNs, ImportContext.Element, string.Empty, null, limiter);
125  if (typeMapping.IsAnonymousType && !typeMapping.TypeDesc.IsSpecial)
126  {
127  throw new InvalidOperationException(Res.GetString("XmlAnonymousInclude", type.FullName));
128  }
129  arrayNestingLevel = num;
130  savedArrayItemAttributes = xmlArrayItemAttributes;
131  savedArrayNamespace = text;
132  }
133 
138  {
139  return ImportTypeMapping(type, null, null);
140  }
141 
146  public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace)
147  {
148  return ImportTypeMapping(type, null, defaultNamespace);
149  }
150 
156  {
157  return ImportTypeMapping(type, root, null);
158  }
159 
165  public XmlTypeMapping ImportTypeMapping(Type type, XmlRootAttribute root, string defaultNamespace)
166  {
167  if (type == null)
168  {
169  throw new ArgumentNullException("type");
170  }
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;
175  }
176 
184  public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement)
185  {
186  return ImportMembersMapping(elementName, ns, members, hasWrapperElement, rpc: false);
187  }
188 
198  public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc)
199  {
200  return ImportMembersMapping(elementName, ns, members, hasWrapperElement, rpc, openModel: false);
201  }
202 
214  public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, bool openModel)
215  {
216  return ImportMembersMapping(elementName, ns, members, hasWrapperElement, rpc, openModel, XmlMappingAccess.Read | XmlMappingAccess.Write);
217  }
218 
231  public XmlMembersMapping ImportMembersMapping(string elementName, string ns, XmlReflectionMember[] members, bool hasWrapperElement, bool rpc, bool openModel, XmlMappingAccess access)
232  {
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()));
237  elementAccessor.Form = XmlSchemaForm.Qualified;
238  if (!rpc)
239  {
240  if (hasWrapperElement)
241  {
242  elementAccessor = (ElementAccessor)ReconcileAccessor(elementAccessor, elements);
243  }
244  else
245  {
246  MemberMapping[] members2 = membersMapping.Members;
247  foreach (MemberMapping memberMapping in members2)
248  {
249  if (memberMapping.Elements != null && memberMapping.Elements.Length != 0)
250  {
251  memberMapping.Elements[0] = (ElementAccessor)ReconcileAccessor(memberMapping.Elements[0], elements);
252  }
253  }
254  }
255  }
256  XmlMembersMapping xmlMembersMapping = new XmlMembersMapping(typeScope, elementAccessor, access);
257  xmlMembersMapping.GenerateSerializer = true;
258  return xmlMembersMapping;
259  }
260 
261  private XmlAttributes GetAttributes(Type type, bool canBeSimpleType)
262  {
263  XmlAttributes xmlAttributes = attributeOverrides[type];
264  if (xmlAttributes != null)
265  {
266  return xmlAttributes;
267  }
268  if (canBeSimpleType && TypeScope.IsKnownType(type))
269  {
270  return defaultAttributes;
271  }
272  return new XmlAttributes(type);
273  }
274 
275  private XmlAttributes GetAttributes(MemberInfo memberInfo)
276  {
277  XmlAttributes xmlAttributes = attributeOverrides[memberInfo.DeclaringType, memberInfo.Name];
278  if (xmlAttributes != null)
279  {
280  return xmlAttributes;
281  }
282  return new XmlAttributes(memberInfo);
283  }
284 
285  private ElementAccessor ImportElement(TypeModel model, XmlRootAttribute root, string defaultNamespace, RecursionLimiter limiter)
286  {
287  XmlAttributes attributes = GetAttributes(model.Type, canBeSimpleType: true);
288  if (root == null)
289  {
290  root = attributes.XmlRoot;
291  }
292  string text = root?.Namespace;
293  if (text == null)
294  {
295  text = defaultNamespace;
296  }
297  if (text == null)
298  {
299  text = defaultNs;
300  }
301  arrayNestingLevel = -1;
302  savedArrayItemAttributes = null;
303  savedArrayNamespace = null;
304  ElementAccessor elementAccessor = CreateElementAccessor(ImportTypeMapping(model, text, ImportContext.Element, string.Empty, attributes, limiter), text);
305  if (root != null)
306  {
307  if (root.ElementName.Length > 0)
308  {
309  elementAccessor.Name = XmlConvert.EncodeLocalName(root.ElementName);
310  }
311  if (root.IsNullableSpecified && !root.IsNullable && model.TypeDesc.IsOptionalValue)
312  {
313  throw new InvalidOperationException(Res.GetString("XmlInvalidNotNullable", model.TypeDesc.BaseTypeDesc.FullName, "XmlRoot"));
314  }
315  elementAccessor.IsNullable = (root.IsNullableSpecified ? root.IsNullable : (model.TypeDesc.IsNullable || model.TypeDesc.IsOptionalValue));
316  CheckNullable(elementAccessor.IsNullable, model.TypeDesc, elementAccessor.Mapping);
317  }
318  else
319  {
320  elementAccessor.IsNullable = (model.TypeDesc.IsNullable || model.TypeDesc.IsOptionalValue);
321  }
322  elementAccessor.Form = XmlSchemaForm.Qualified;
323  return (ElementAccessor)ReconcileAccessor(elementAccessor, elements);
324  }
325 
326  private static string GetMappingName(Mapping mapping)
327  {
328  if (mapping is MembersMapping)
329  {
330  return "(method)";
331  }
332  if (mapping is TypeMapping)
333  {
334  return ((TypeMapping)mapping).TypeDesc.FullName;
335  }
336  throw new ArgumentException(Res.GetString("XmlInternalError"), "mapping");
337  }
338 
339  private ElementAccessor ReconcileLocalAccessor(ElementAccessor accessor, string ns)
340  {
341  if (accessor.Namespace == ns)
342  {
343  return accessor;
344  }
345  return (ElementAccessor)ReconcileAccessor(accessor, elements);
346  }
347 
348  private Accessor ReconcileAccessor(Accessor accessor, NameTable accessors)
349  {
350  if (accessor.Any && accessor.Name.Length == 0)
351  {
352  return accessor;
353  }
354  Accessor accessor2 = (Accessor)accessors[accessor.Name, accessor.Namespace];
355  if (accessor2 == null)
356  {
357  accessor.IsTopLevelInSchema = true;
358  accessors.Add(accessor.Name, accessor.Namespace, accessor);
359  return accessor;
360  }
361  if (accessor2.Mapping == accessor.Mapping)
362  {
363  return accessor2;
364  }
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)))
366  {
367  string text = Convert.ToString(accessor.Default, CultureInfo.InvariantCulture);
368  string text2 = Convert.ToString(accessor2.Default, CultureInfo.InvariantCulture);
369  if (text == text2)
370  {
371  return accessor2;
372  }
373  throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAccessorDefault", accessor.Name, accessor.Namespace, text, text2));
374  }
375  if (accessor.Mapping is MembersMapping || accessor2.Mapping is MembersMapping)
376  {
377  throw new InvalidOperationException(Res.GetString("XmlMethodTypeNameConflict", accessor.Name, accessor.Namespace));
378  }
379  if (accessor.Mapping is ArrayMapping)
380  {
381  if (!(accessor2.Mapping is ArrayMapping))
382  {
383  throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAccessor", accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping)));
384  }
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)
389  {
390  if (arrayMapping2 == accessor.Mapping)
391  {
392  return accessor2;
393  }
394  arrayMapping2 = arrayMapping2.Next;
395  }
396  arrayMapping.Next = next;
397  if (!arrayMapping.IsAnonymousType)
398  {
399  types[accessor2.Mapping.TypeName, accessor2.Mapping.Namespace] = arrayMapping;
400  }
401  return accessor2;
402  }
403  if (accessor is AttributeAccessor)
404  {
405  throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAttributeAccessor", accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping)));
406  }
407  throw new InvalidOperationException(Res.GetString("XmlCannotReconcileAccessor", accessor.Name, accessor.Namespace, GetMappingName(accessor2.Mapping), GetMappingName(accessor.Mapping)));
408  }
409 
410  private Exception CreateReflectionException(string context, Exception e)
411  {
412  return new InvalidOperationException(Res.GetString("XmlReflectionError", context), e);
413  }
414 
415  private Exception CreateTypeReflectionException(string context, Exception e)
416  {
417  return new InvalidOperationException(Res.GetString("XmlTypeReflectionError", context), e);
418  }
419 
420  private Exception CreateMemberReflectionException(FieldModel model, Exception e)
421  {
422  return new InvalidOperationException(Res.GetString(model.IsProperty ? "XmlPropertyReflectionError" : "XmlFieldReflectionError", model.Name), e);
423  }
424 
425  private TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType, XmlAttributes a, RecursionLimiter limiter)
426  {
427  return ImportTypeMapping(model, ns, context, dataType, a, repeats: false, openModel: false, limiter);
428  }
429 
430  private TypeMapping ImportTypeMapping(TypeModel model, string ns, ImportContext context, string dataType, XmlAttributes a, bool repeats, bool openModel, RecursionLimiter limiter)
431  {
432  try
433  {
434  if (dataType.Length > 0)
435  {
436  TypeDesc typeDesc = TypeScope.IsOptionalValue(model.Type) ? model.TypeDesc.BaseTypeDesc : model.TypeDesc;
437  if (!typeDesc.IsPrimitive)
438  {
439  throw new InvalidOperationException(Res.GetString("XmlInvalidDataTypeUsage", dataType, "XmlElementAttribute.DataType"));
440  }
441  TypeDesc typeDesc2 = typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema");
442  if (typeDesc2 == null)
443  {
444  throw new InvalidOperationException(Res.GetString("XmlInvalidXsdDataType", dataType, "XmlElementAttribute.DataType", new XmlQualifiedName(dataType, "http://www.w3.org/2001/XMLSchema").ToString()));
445  }
446  if (typeDesc.FullName != typeDesc2.FullName)
447  {
448  throw new InvalidOperationException(Res.GetString("XmlDataTypeMismatch", dataType, "XmlElementAttribute.DataType", typeDesc.FullName));
449  }
450  }
451  if (a == null)
452  {
453  a = GetAttributes(model.Type, canBeSimpleType: false);
454  }
455  if ((a.XmlFlags & (XmlAttributeFlags)(-193)) != 0)
456  {
457  throw new InvalidOperationException(Res.GetString("XmlInvalidTypeAttributes", model.Type.FullName));
458  }
459  switch (model.TypeDesc.Kind)
460  {
461  case TypeKind.Enum:
462  return ImportEnumMapping((EnumModel)model, ns, repeats);
463  case TypeKind.Primitive:
464  if (a.XmlFlags != 0)
465  {
466  throw InvalidAttributeUseException(model.Type);
467  }
468  return ImportPrimitiveMapping((PrimitiveModel)model, context, dataType, repeats);
469  case TypeKind.Array:
470  case TypeKind.Collection:
471  case TypeKind.Enumerable:
472  {
473  if (context != ImportContext.Element)
474  {
475  throw UnsupportedException(model.TypeDesc, context);
476  }
477  arrayNestingLevel++;
478  ArrayMapping result = ImportArrayLikeMapping((ArrayModel)model, ns, limiter);
479  arrayNestingLevel--;
480  return result;
481  }
482  case TypeKind.Root:
483  case TypeKind.Struct:
484  case TypeKind.Class:
485  if (context != ImportContext.Element)
486  {
487  throw UnsupportedException(model.TypeDesc, context);
488  }
489  if (model.TypeDesc.IsOptionalValue)
490  {
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)
495  {
496  typeMapping = ImportTypeMapping(modelScope.GetTypeModel(model.TypeDesc.BaseTypeDesc.Type), ns, context, dataType, null, repeats, openModel, limiter);
497  }
498  return CreateNullableMapping(typeMapping, model.TypeDesc.Type);
499  }
500  return ImportStructLikeMapping((StructModel)model, ns, openModel, a, limiter);
501  default:
502  if (model.TypeDesc.Kind == TypeKind.Serializable)
503  {
504  if ((a.XmlFlags & (XmlAttributeFlags)(-65)) != 0)
505  {
506  throw new InvalidOperationException(Res.GetString("XmlSerializableAttributes", model.TypeDesc.FullName, typeof(XmlSchemaProviderAttribute).Name));
507  }
508  }
509  else if (a.XmlFlags != 0)
510  {
511  throw InvalidAttributeUseException(model.Type);
512  }
513  if (!model.TypeDesc.IsSpecial)
514  {
515  throw UnsupportedException(model.TypeDesc, context);
516  }
517  return ImportSpecialMapping(model.Type, model.TypeDesc, ns, context, limiter);
518  }
519  }
520  catch (Exception ex)
521  {
522  if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
523  {
524  throw;
525  }
526  throw CreateTypeReflectionException(model.TypeDesc.FullName, ex);
527  }
528  }
529 
530  internal static MethodInfo GetMethodFromSchemaProvider(XmlSchemaProviderAttribute provider, Type type)
531  {
532  if (provider.IsAny)
533  {
534  return null;
535  }
536  if (provider.MethodName == null)
537  {
538  throw new ArgumentNullException("MethodName");
539  }
540  if (!CodeGenerator.IsValidLanguageIndependentIdentifier(provider.MethodName))
541  {
542  throw new ArgumentException(Res.GetString("XmlGetSchemaMethodName", provider.MethodName), "MethodName");
543  }
544  MethodInfo method = method = type.GetMethod(provider.MethodName, BindingFlags.Static | BindingFlags.Public, null, new Type[1]
545  {
546  typeof(XmlSchemaSet)
547  }, null);
548  if (method == null)
549  {
550  throw new InvalidOperationException(Res.GetString("XmlGetSchemaMethodMissing", provider.MethodName, typeof(XmlSchemaSet).Name, type.FullName));
551  }
552  if (!typeof(XmlQualifiedName).IsAssignableFrom(method.ReturnType) && !typeof(XmlSchemaType).IsAssignableFrom(method.ReturnType))
553  {
554  throw new InvalidOperationException(Res.GetString("XmlGetSchemaMethodReturnType", type.Name, provider.MethodName, typeof(XmlSchemaProviderAttribute).Name, typeof(XmlQualifiedName).FullName, typeof(XmlSchemaType).FullName));
555  }
556  return method;
557  }
558 
559  private SpecialMapping ImportSpecialMapping(Type type, TypeDesc typeDesc, string ns, ImportContext context, RecursionLimiter limiter)
560  {
561  if (specials == null)
562  {
563  specials = new Hashtable();
564  }
565  SpecialMapping specialMapping = (SpecialMapping)specials[type];
566  if (specialMapping != null)
567  {
568  CheckContext(specialMapping.TypeDesc, context);
569  return specialMapping;
570  }
571  if (typeDesc.Kind == TypeKind.Serializable)
572  {
573  SerializableMapping serializableMapping = null;
574  object[] customAttributes = type.GetCustomAttributes(typeof(XmlSchemaProviderAttribute), inherit: false);
575  if (customAttributes.Length != 0)
576  {
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)
582  {
583  if (serializables == null)
584  {
585  serializables = new NameTable();
586  }
587  SerializableMapping serializableMapping2 = (SerializableMapping)serializables[xsiType];
588  if (serializableMapping2 != null)
589  {
590  if (serializableMapping2.Type == null)
591  {
592  serializableMapping = serializableMapping2;
593  }
594  else if (serializableMapping2.Type != type)
595  {
596  SerializableMapping next = serializableMapping2.Next;
597  serializableMapping2.Next = serializableMapping;
598  serializableMapping.Next = next;
599  }
600  }
601  else
602  {
603  XmlSchemaType xsdType = serializableMapping.XsdType;
604  if (xsdType != null)
605  {
606  SetBase(serializableMapping, xsdType.DerivedFrom);
607  }
608  serializables[xsiType] = serializableMapping;
609  }
610  serializableMapping.TypeName = xsiType.Name;
611  serializableMapping.Namespace = xsiType.Namespace;
612  }
613  serializableMapping.TypeDesc = typeDesc;
614  serializableMapping.Type = type;
615  IncludeTypes(type);
616  }
617  else
618  {
619  serializableMapping = new SerializableMapping();
620  serializableMapping.TypeDesc = typeDesc;
621  serializableMapping.Type = type;
622  }
623  specialMapping = serializableMapping;
624  }
625  else
626  {
627  specialMapping = new SpecialMapping();
628  specialMapping.TypeDesc = typeDesc;
629  }
630  CheckContext(typeDesc, context);
631  specials.Add(type, specialMapping);
632  typeScope.AddTypeMapping(specialMapping);
633  return specialMapping;
634  }
635 
636  internal static void ValidationCallbackWithErrorCode(object sender, ValidationEventArgs args)
637  {
638  if (args.Severity == XmlSeverityType.Error)
639  {
640  throw new InvalidOperationException(Res.GetString("XmlSerializableSchemaError", typeof(IXmlSerializable).Name, args.Message));
641  }
642  }
643 
644  internal void SetBase(SerializableMapping mapping, XmlQualifiedName baseQname)
645  {
646  if (!baseQname.IsEmpty && !(baseQname.Namespace == "http://www.w3.org/2001/XMLSchema"))
647  {
648  XmlSchemaSet schemas = mapping.Schemas;
649  ArrayList arrayList = (ArrayList)schemas.Schemas(baseQname.Namespace);
650  if (arrayList.Count == 0)
651  {
652  throw new InvalidOperationException(Res.GetString("XmlMissingSchema", baseQname.Namespace));
653  }
654  if (arrayList.Count > 1)
655  {
656  throw new InvalidOperationException(Res.GetString("XmlGetSchemaInclude", baseQname.Namespace, typeof(IXmlSerializable).Name, "GetSchema"));
657  }
658  XmlSchema xmlSchema = (XmlSchema)arrayList[0];
659  XmlSchemaType xmlSchemaType = (XmlSchemaType)xmlSchema.SchemaTypes[baseQname];
660  xmlSchemaType = ((xmlSchemaType.Redefined != null) ? xmlSchemaType.Redefined : xmlSchemaType);
661  if (serializables[baseQname] == null)
662  {
663  SerializableMapping serializableMapping = new SerializableMapping(baseQname, schemas);
664  SetBase(serializableMapping, xmlSchemaType.DerivedFrom);
665  serializables.Add(baseQname, serializableMapping);
666  }
667  mapping.SetBaseMapping((SerializableMapping)serializables[baseQname]);
668  }
669  }
670 
671  private static string GetContextName(ImportContext context)
672  {
673  switch (context)
674  {
675  case ImportContext.Element:
676  return "element";
677  case ImportContext.Attribute:
678  return "attribute";
679  case ImportContext.Text:
680  return "text";
681  default:
682  throw new ArgumentException(Res.GetString("XmlInternalError"), "context");
683  }
684  }
685 
686  private static Exception InvalidAttributeUseException(Type type)
687  {
688  return new InvalidOperationException(Res.GetString("XmlInvalidAttributeUse", type.FullName));
689  }
690 
691  private static Exception UnsupportedException(TypeDesc typeDesc, ImportContext context)
692  {
693  return new InvalidOperationException(Res.GetString("XmlIllegalTypeContext", typeDesc.FullName, GetContextName(context)));
694  }
695 
696  private StructMapping CreateRootMapping()
697  {
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;
706  }
707 
708  private NullableMapping CreateNullableMapping(TypeMapping baseMapping, Type type)
709  {
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)
714  {
715  if (typeMapping is NullableMapping)
716  {
717  nullableMapping = (NullableMapping)typeMapping;
718  if (nullableMapping.BaseMapping is PrimitiveMapping && baseMapping is PrimitiveMapping)
719  {
720  return nullableMapping;
721  }
722  if (nullableMapping.BaseMapping == baseMapping)
723  {
724  return nullableMapping;
725  }
726  throw new InvalidOperationException(Res.GetString("XmlTypesDuplicate", nullableTypeDesc.FullName, typeMapping.TypeDesc.FullName, nullableTypeDesc.Name, typeMapping.Namespace));
727  }
728  throw new InvalidOperationException(Res.GetString("XmlTypesDuplicate", nullableTypeDesc.FullName, typeMapping.TypeDesc.FullName, nullableTypeDesc.Name, typeMapping.Namespace));
729  }
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)
737  {
738  nullables.Add(baseMapping.TypeName, baseMapping.Namespace, nullableMapping);
739  }
740  else
741  {
742  anonymous[type] = nullableMapping;
743  }
744  typeScope.AddTypeMapping(nullableMapping);
745  return nullableMapping;
746  }
747 
748  private StructMapping GetRootMapping()
749  {
750  if (root == null)
751  {
752  root = CreateRootMapping();
753  typeScope.AddTypeMapping(root);
754  }
755  return root;
756  }
757 
758  private TypeMapping GetTypeMapping(string typeName, string ns, TypeDesc typeDesc, NameTable typeLib, Type type)
759  {
760  TypeMapping typeMapping = (typeName != null && typeName.Length != 0) ? ((TypeMapping)typeLib[typeName, ns]) : ((type == null) ? null : ((TypeMapping)anonymous[type]));
761  if (typeMapping == null)
762  {
763  return null;
764  }
765  if (!typeMapping.IsAnonymousType && typeMapping.TypeDesc != typeDesc)
766  {
767  throw new InvalidOperationException(Res.GetString("XmlTypesDuplicate", typeDesc.FullName, typeMapping.TypeDesc.FullName, typeName, ns));
768  }
769  return typeMapping;
770  }
771 
772  private StructMapping ImportStructLikeMapping(StructModel model, string ns, bool openModel, XmlAttributes a, RecursionLimiter limiter)
773  {
774  if (model.TypeDesc.Kind == TypeKind.Root)
775  {
776  return GetRootMapping();
777  }
778  if (a == null)
779  {
780  a = GetAttributes(model.Type, canBeSimpleType: false);
781  }
782  string text = ns;
783  if (a.XmlType != null && a.XmlType.Namespace != null)
784  {
785  text = a.XmlType.Namespace;
786  }
787  else if (a.XmlRoot != null && a.XmlRoot.Namespace != null)
788  {
789  text = a.XmlRoot.Namespace;
790  }
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)
795  {
796  structMapping = new StructMapping();
797  structMapping.TypeDesc = model.TypeDesc;
798  structMapping.Namespace = text;
799  structMapping.TypeName = name;
800  if (!structMapping.IsAnonymousType)
801  {
802  types.Add(name, text, structMapping);
803  }
804  else
805  {
806  anonymous[model.Type] = structMapping;
807  }
808  if (a.XmlType != null)
809  {
810  structMapping.IncludeInSchema = a.XmlType.IncludeInSchema;
811  }
812  if (limiter.IsExceededLimit)
813  {
814  limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, structMapping));
815  return structMapping;
816  }
817  limiter.Depth++;
818  InitializeStructMembers(structMapping, model, openModel, name, limiter);
819  while (limiter.DeferredWorkItems.Count > 0)
820  {
821  int index = limiter.DeferredWorkItems.Count - 1;
822  ImportStructWorkItem importStructWorkItem = limiter.DeferredWorkItems[index];
823  if (InitializeStructMembers(importStructWorkItem.Mapping, importStructWorkItem.Model, openModel, name, limiter))
824  {
825  limiter.DeferredWorkItems.RemoveAt(index);
826  }
827  }
828  limiter.Depth--;
829  }
830  return structMapping;
831  }
832 
833  private bool InitializeStructMembers(StructMapping mapping, StructModel model, bool openModel, string typeName, RecursionLimiter limiter)
834  {
835  if (mapping.IsFullyInitialized)
836  {
837  return true;
838  }
839  if (model.TypeDesc.BaseTypeDesc != null)
840  {
841  TypeModel typeModel = modelScope.GetTypeModel(model.Type.BaseType, directReference: false);
842  if (!(typeModel is StructModel))
843  {
844  throw new NotSupportedException(Res.GetString("XmlUnsupportedInheritance", model.Type.BaseType.FullName));
845  }
846  StructMapping structMapping = ImportStructLikeMapping((StructModel)typeModel, mapping.Namespace, openModel, null, limiter);
847  int num = limiter.DeferredWorkItems.IndexOf(structMapping);
848  if (num >= 0)
849  {
850  if (!limiter.DeferredWorkItems.Contains(mapping))
851  {
852  limiter.DeferredWorkItems.Add(new ImportStructWorkItem(model, mapping));
853  }
854  int num2 = limiter.DeferredWorkItems.Count - 1;
855  if (num < num2)
856  {
857  ImportStructWorkItem value = limiter.DeferredWorkItems[num];
858  limiter.DeferredWorkItems[num] = limiter.DeferredWorkItems[num2];
859  limiter.DeferredWorkItems[num2] = value;
860  }
861  return false;
862  }
863  mapping.BaseMapping = structMapping;
864  ICollection values = mapping.BaseMapping.LocalAttributes.Values;
865  foreach (AttributeAccessor item in values)
866  {
867  AddUniqueAccessor(mapping.LocalAttributes, item);
868  }
869  if (!mapping.BaseMapping.HasExplicitSequence())
870  {
871  values = mapping.BaseMapping.LocalElements.Values;
872  foreach (ElementAccessor item2 in values)
873  {
874  AddUniqueAccessor(mapping.LocalElements, item2);
875  }
876  }
877  }
878  ArrayList arrayList = new ArrayList();
879  TextAccessor textAccessor = null;
880  bool hasElements = false;
881  bool flag = false;
882  MemberInfo[] memberInfos = model.GetMemberInfos();
883  foreach (MemberInfo memberInfo in memberInfos)
884  {
885  if ((memberInfo.MemberType & (MemberTypes.Field | MemberTypes.Property)) == (MemberTypes)0)
886  {
887  continue;
888  }
889  XmlAttributes attributes = GetAttributes(memberInfo);
890  if (!attributes.XmlIgnore)
891  {
892  FieldModel fieldModel = model.GetFieldModel(memberInfo);
893  if (fieldModel != null)
894  {
895  try
896  {
897  MemberMapping memberMapping = ImportFieldMapping(model, fieldModel, attributes, mapping.Namespace, limiter);
898  if (memberMapping != null && (mapping.BaseMapping == null || !mapping.BaseMapping.Declares(memberMapping, mapping.TypeName)))
899  {
900  flag |= memberMapping.IsSequence;
901  AddUniqueAccessor(memberMapping, mapping.LocalElements, mapping.LocalAttributes, flag);
902  if (memberMapping.Text != null)
903  {
904  if (!memberMapping.Text.Mapping.TypeDesc.CanBeTextValue && memberMapping.Text.Mapping.IsList)
905  {
906  throw new InvalidOperationException(Res.GetString("XmlIllegalTypedTextAttribute", typeName, memberMapping.Text.Name, memberMapping.Text.Mapping.TypeDesc.FullName));
907  }
908  if (textAccessor != null)
909  {
910  throw new InvalidOperationException(Res.GetString("XmlIllegalMultipleText", model.Type.FullName));
911  }
912  textAccessor = memberMapping.Text;
913  }
914  if (memberMapping.Xmlns != null)
915  {
916  if (mapping.XmlnsMember != null)
917  {
918  throw new InvalidOperationException(Res.GetString("XmlMultipleXmlns", model.Type.FullName));
919  }
920  mapping.XmlnsMember = memberMapping;
921  }
922  if (memberMapping.Elements != null && memberMapping.Elements.Length != 0)
923  {
924  hasElements = true;
925  }
926  arrayList.Add(memberMapping);
927  }
928  }
929  catch (Exception ex)
930  {
931  if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
932  {
933  throw;
934  }
935  throw CreateMemberReflectionException(fieldModel, ex);
936  }
937  }
938  }
939  }
940  mapping.SetContentModel(textAccessor, hasElements);
941  if (flag)
942  {
943  Hashtable hashtable = new Hashtable();
944  for (int j = 0; j < arrayList.Count; j++)
945  {
946  MemberMapping memberMapping2 = (MemberMapping)arrayList[j];
947  if (memberMapping2.IsParticle)
948  {
949  if (!memberMapping2.IsSequence)
950  {
951  throw new InvalidOperationException(Res.GetString("XmlSequenceInconsistent", "Order", memberMapping2.Name));
952  }
953  if (hashtable[memberMapping2.SequenceId] != null)
954  {
955  throw new InvalidOperationException(Res.GetString("XmlSequenceUnique", memberMapping2.SequenceId.ToString(CultureInfo.InvariantCulture), "Order", memberMapping2.Name));
956  }
957  hashtable[memberMapping2.SequenceId] = memberMapping2;
958  }
959  }
960  arrayList.Sort(new MemberMappingComparer());
961  }
962  mapping.Members = (MemberMapping[])arrayList.ToArray(typeof(MemberMapping));
963  if (mapping.BaseMapping == null)
964  {
965  mapping.BaseMapping = GetRootMapping();
966  }
967  if (mapping.XmlnsMember != null && mapping.BaseMapping.HasXmlnsMember)
968  {
969  throw new InvalidOperationException(Res.GetString("XmlMultipleXmlns", model.Type.FullName));
970  }
971  IncludeTypes(model.Type, limiter);
972  typeScope.AddTypeMapping(mapping);
973  if (openModel)
974  {
975  mapping.IsOpenModel = true;
976  }
977  return true;
978  }
979 
980  private static bool IsAnonymousType(XmlAttributes a, string contextNs)
981  {
982  if (a.XmlType != null && a.XmlType.AnonymousType)
983  {
984  string @namespace = a.XmlType.Namespace;
985  if (!string.IsNullOrEmpty(@namespace))
986  {
987  return @namespace == contextNs;
988  }
989  return true;
990  }
991  return false;
992  }
993 
994  internal string XsdTypeName(Type type)
995  {
996  if (type == typeof(object))
997  {
998  return "anyType";
999  }
1000  TypeDesc typeDesc = typeScope.GetTypeDesc(type);
1001  if (typeDesc.IsPrimitive && typeDesc.DataType != null && typeDesc.DataType.Name != null && typeDesc.DataType.Name.Length > 0)
1002  {
1003  return typeDesc.DataType.Name;
1004  }
1005  return XsdTypeName(type, GetAttributes(type, canBeSimpleType: false), typeDesc.Name);
1006  }
1007 
1008  internal string XsdTypeName(Type type, XmlAttributes a, string name)
1009  {
1010  string text = name;
1011  if (a.XmlType != null && a.XmlType.TypeName.Length > 0)
1012  {
1013  text = a.XmlType.TypeName;
1014  }
1015  if (type.IsGenericType && text.IndexOf('{') >= 0)
1016  {
1017  Type genericTypeDefinition = type.GetGenericTypeDefinition();
1018  Type[] genericArguments = genericTypeDefinition.GetGenericArguments();
1019  Type[] genericArguments2 = type.GetGenericArguments();
1020  for (int i = 0; i < genericArguments.Length; i++)
1021  {
1022  string text2 = "{" + genericArguments[i] + "}";
1023  if (text.Contains(text2))
1024  {
1025  text = text.Replace(text2, XsdTypeName(genericArguments2[i]));
1026  if (text.IndexOf('{') < 0)
1027  {
1028  break;
1029  }
1030  }
1031  }
1032  }
1033  return text;
1034  }
1035 
1036  private static int CountAtLevel(XmlArrayItemAttributes attributes, int level)
1037  {
1038  int num = 0;
1039  for (int i = 0; i < attributes.Count; i++)
1040  {
1041  if (attributes[i].NestingLevel == level)
1042  {
1043  num++;
1044  }
1045  }
1046  return num;
1047  }
1048 
1049  private void SetArrayMappingType(ArrayMapping mapping, string defaultNs, Type type)
1050  {
1051  XmlAttributes attributes = GetAttributes(type, canBeSimpleType: false);
1052  if (IsAnonymousType(attributes, defaultNs))
1053  {
1054  mapping.TypeName = null;
1055  mapping.Namespace = defaultNs;
1056  return;
1057  }
1058  ElementAccessor elementAccessor = null;
1059  TypeMapping typeMapping;
1060  if (mapping.Elements.Length == 1)
1061  {
1062  elementAccessor = mapping.Elements[0];
1063  typeMapping = elementAccessor.Mapping;
1064  }
1065  else
1066  {
1067  typeMapping = null;
1068  }
1069  bool flag = true;
1070  string text;
1071  string name;
1072  if (attributes.XmlType != null)
1073  {
1074  text = attributes.XmlType.Namespace;
1075  name = XsdTypeName(type, attributes, attributes.XmlType.TypeName);
1076  name = XmlConvert.EncodeLocalName(name);
1077  flag = (name == null);
1078  }
1079  else if (typeMapping is EnumMapping)
1080  {
1081  text = typeMapping.Namespace;
1082  name = typeMapping.DefaultElementName;
1083  }
1084  else if (typeMapping is PrimitiveMapping)
1085  {
1086  text = defaultNs;
1087  name = typeMapping.TypeDesc.DataType.Name;
1088  }
1089  else if (typeMapping is StructMapping && typeMapping.TypeDesc.IsRoot)
1090  {
1091  text = defaultNs;
1092  name = "anyType";
1093  }
1094  else if (typeMapping != null)
1095  {
1096  text = ((typeMapping.Namespace == "http://www.w3.org/2001/XMLSchema") ? defaultNs : typeMapping.Namespace);
1097  name = typeMapping.DefaultElementName;
1098  }
1099  else
1100  {
1101  text = defaultNs;
1102  name = "Choice" + choiceNum++;
1103  }
1104  if (name == null)
1105  {
1106  name = "Any";
1107  }
1108  if (elementAccessor != null)
1109  {
1110  text = elementAccessor.Namespace;
1111  }
1112  if (text == null)
1113  {
1114  text = defaultNs;
1115  }
1116  string text2 = name = (flag ? ("ArrayOf" + CodeIdentifier.MakePascal(name)) : name);
1117  int num = 1;
1118  TypeMapping typeMapping2 = (TypeMapping)types[text2, text];
1119  while (typeMapping2 != null)
1120  {
1121  if (typeMapping2 is ArrayMapping)
1122  {
1123  ArrayMapping arrayMapping = (ArrayMapping)typeMapping2;
1124  if (AccessorMapping.ElementsMatch(arrayMapping.Elements, mapping.Elements))
1125  {
1126  break;
1127  }
1128  }
1129  text2 = name + num.ToString(CultureInfo.InvariantCulture);
1130  typeMapping2 = (TypeMapping)types[text2, text];
1131  num++;
1132  }
1133  mapping.TypeName = text2;
1134  mapping.Namespace = text;
1135  }
1136 
1137  private ArrayMapping ImportArrayLikeMapping(ArrayModel model, string ns, RecursionLimiter limiter)
1138  {
1139  ArrayMapping arrayMapping = new ArrayMapping();
1140  arrayMapping.TypeDesc = model.TypeDesc;
1141  if (savedArrayItemAttributes == null)
1142  {
1143  savedArrayItemAttributes = new XmlArrayItemAttributes();
1144  }
1145  if (CountAtLevel(savedArrayItemAttributes, arrayNestingLevel) == 0)
1146  {
1147  savedArrayItemAttributes.Add(CreateArrayItemAttribute(typeScope.GetTypeDesc(model.Element.Type), arrayNestingLevel));
1148  }
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++)
1152  {
1153  arrayMapping.Elements[i] = ReconcileLocalAccessor(arrayMapping.Elements[i], arrayMapping.Namespace);
1154  }
1155  IncludeTypes(model.Type);
1156  ArrayMapping arrayMapping2 = (ArrayMapping)types[arrayMapping.TypeName, arrayMapping.Namespace];
1157  if (arrayMapping2 != null)
1158  {
1159  ArrayMapping next = arrayMapping2;
1160  while (arrayMapping2 != null)
1161  {
1162  if (arrayMapping2.TypeDesc == model.TypeDesc)
1163  {
1164  return arrayMapping2;
1165  }
1166  arrayMapping2 = arrayMapping2.Next;
1167  }
1168  arrayMapping.Next = next;
1169  if (!arrayMapping.IsAnonymousType)
1170  {
1171  types[arrayMapping.TypeName, arrayMapping.Namespace] = arrayMapping;
1172  }
1173  else
1174  {
1175  anonymous[model.Type] = arrayMapping;
1176  }
1177  return arrayMapping;
1178  }
1179  typeScope.AddTypeMapping(arrayMapping);
1180  if (!arrayMapping.IsAnonymousType)
1181  {
1182  types.Add(arrayMapping.TypeName, arrayMapping.Namespace, arrayMapping);
1183  }
1184  else
1185  {
1186  anonymous[model.Type] = arrayMapping;
1187  }
1188  return arrayMapping;
1189  }
1190 
1191  private void CheckContext(TypeDesc typeDesc, ImportContext context)
1192  {
1193  switch (context)
1194  {
1195  case ImportContext.Element:
1196  if (typeDesc.CanBeElementValue)
1197  {
1198  return;
1199  }
1200  break;
1201  case ImportContext.Attribute:
1202  if (typeDesc.CanBeAttributeValue)
1203  {
1204  return;
1205  }
1206  break;
1207  case ImportContext.Text:
1208  if (typeDesc.CanBeTextValue || typeDesc.IsEnum || typeDesc.IsPrimitive)
1209  {
1210  return;
1211  }
1212  break;
1213  default:
1214  throw new ArgumentException(Res.GetString("XmlInternalError"), "context");
1215  }
1216  throw UnsupportedException(typeDesc, context);
1217  }
1218 
1219  private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, ImportContext context, string dataType, bool repeats)
1220  {
1221  PrimitiveMapping primitiveMapping = new PrimitiveMapping();
1222  if (dataType.Length > 0)
1223  {
1224  primitiveMapping.TypeDesc = typeScope.GetTypeDesc(dataType, "http://www.w3.org/2001/XMLSchema");
1225  if (primitiveMapping.TypeDesc == null)
1226  {
1227  primitiveMapping.TypeDesc = typeScope.GetTypeDesc(dataType, "http://microsoft.com/wsdl/types/");
1228  if (primitiveMapping.TypeDesc == null)
1229  {
1230  throw new InvalidOperationException(Res.GetString("XmlUdeclaredXsdType", dataType));
1231  }
1232  }
1233  }
1234  else
1235  {
1236  primitiveMapping.TypeDesc = model.TypeDesc;
1237  }
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;
1243  }
1244 
1245  private EnumMapping ImportEnumMapping(EnumModel model, string ns, bool repeats)
1246  {
1247  XmlAttributes attributes = GetAttributes(model.Type, canBeSimpleType: false);
1248  string text = ns;
1249  if (attributes.XmlType != null && attributes.XmlType.Namespace != null)
1250  {
1251  text = attributes.XmlType.Namespace;
1252  }
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)
1257  {
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)
1264  {
1265  throw new InvalidOperationException(Res.GetString("XmlIllegalAttributeFlagsArray", model.TypeDesc.FullName));
1266  }
1267  enumMapping.IsList = repeats;
1268  enumMapping.IncludeInSchema = (attributes.XmlType == null || attributes.XmlType.IncludeInSchema);
1269  if (!enumMapping.IsAnonymousType)
1270  {
1271  types.Add(name, text, enumMapping);
1272  }
1273  else
1274  {
1275  anonymous[model.Type] = enumMapping;
1276  }
1277  ArrayList arrayList = new ArrayList();
1278  for (int i = 0; i < model.Constants.Length; i++)
1279  {
1280  ConstantMapping constantMapping = ImportConstantMapping(model.Constants[i]);
1281  if (constantMapping != null)
1282  {
1283  arrayList.Add(constantMapping);
1284  }
1285  }
1286  if (arrayList.Count == 0)
1287  {
1288  throw new InvalidOperationException(Res.GetString("XmlNoSerializableMembers", model.TypeDesc.FullName));
1289  }
1290  enumMapping.Constants = (ConstantMapping[])arrayList.ToArray(typeof(ConstantMapping));
1291  typeScope.AddTypeMapping(enumMapping);
1292  }
1293  return enumMapping;
1294  }
1295 
1296  private ConstantMapping ImportConstantMapping(ConstantModel model)
1297  {
1298  XmlAttributes attributes = GetAttributes(model.FieldInfo);
1299  if (attributes.XmlIgnore)
1300  {
1301  return null;
1302  }
1303  if ((attributes.XmlFlags & (XmlAttributeFlags)(-2)) != 0)
1304  {
1305  throw new InvalidOperationException(Res.GetString("XmlInvalidConstantAttribute"));
1306  }
1307  if (attributes.XmlEnum == null)
1308  {
1309  attributes.XmlEnum = new XmlEnumAttribute();
1310  }
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;
1316  }
1317 
1318  private MembersMapping ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, string ns, bool hasWrapperElement, bool rpc, bool openModel, RecursionLimiter limiter)
1319  {
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;
1326  bool flag = false;
1327  for (int i = 0; i < array.Length; i++)
1328  {
1329  try
1330  {
1331  MemberMapping memberMapping = ImportMemberMapping(xmlReflectionMembers[i], ns, xmlReflectionMembers, rpc, openModel, limiter);
1332  if (!hasWrapperElement && memberMapping.Attribute != null)
1333  {
1334  if (rpc)
1335  {
1336  throw new InvalidOperationException(Res.GetString("XmlRpcLitAttributeAttributes"));
1337  }
1338  throw new InvalidOperationException(Res.GetString("XmlInvalidAttributeType", "XmlAttribute"));
1339  }
1340  if (rpc && xmlReflectionMembers[i].IsReturnValue)
1341  {
1342  if (i > 0)
1343  {
1344  throw new InvalidOperationException(Res.GetString("XmlInvalidReturnPosition"));
1345  }
1346  memberMapping.IsReturnValue = true;
1347  }
1348  array[i] = memberMapping;
1349  flag |= memberMapping.IsSequence;
1350  if (!xmlReflectionMembers[i].XmlAttributes.XmlIgnore)
1351  {
1352  AddUniqueAccessor(memberMapping, nameTable, attributes, flag);
1353  }
1354  array[i] = memberMapping;
1355  if (memberMapping.Text != null)
1356  {
1357  if (textAccessor != null)
1358  {
1359  throw new InvalidOperationException(Res.GetString("XmlIllegalMultipleTextMembers"));
1360  }
1361  textAccessor = memberMapping.Text;
1362  }
1363  if (memberMapping.Xmlns != null)
1364  {
1365  if (membersMapping.XmlnsMember != null)
1366  {
1367  throw new InvalidOperationException(Res.GetString("XmlMultipleXmlnsMembers"));
1368  }
1369  membersMapping.XmlnsMember = memberMapping;
1370  }
1371  }
1372  catch (Exception ex)
1373  {
1374  if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
1375  {
1376  throw;
1377  }
1378  throw CreateReflectionException(xmlReflectionMembers[i].MemberName, ex);
1379  }
1380  }
1381  if (flag)
1382  {
1383  throw new InvalidOperationException(Res.GetString("XmlSequenceMembers", "Order"));
1384  }
1385  membersMapping.Members = array;
1386  membersMapping.HasWrapperElement = hasWrapperElement;
1387  return membersMapping;
1388  }
1389 
1390  private MemberMapping ImportMemberMapping(XmlReflectionMember xmlReflectionMember, string ns, XmlReflectionMember[] xmlReflectionMembers, bool rpc, bool openModel, RecursionLimiter limiter)
1391  {
1392  XmlSchemaForm form = (!rpc) ? XmlSchemaForm.Qualified : XmlSchemaForm.Unqualified;
1393  XmlAttributes xmlAttributes = xmlReflectionMember.XmlAttributes;
1394  TypeDesc typeDesc = typeScope.GetTypeDesc(xmlReflectionMember.MemberType);
1395  if (xmlAttributes.XmlFlags == (XmlAttributeFlags)0)
1396  {
1397  if (typeDesc.IsArrayLike)
1398  {
1399  XmlArrayAttribute xmlArrayAttribute = CreateArrayAttribute(typeDesc);
1400  xmlArrayAttribute.ElementName = xmlReflectionMember.MemberName;
1401  xmlArrayAttribute.Namespace = (rpc ? null : ns);
1402  xmlArrayAttribute.Form = form;
1403  xmlAttributes.XmlArray = xmlArrayAttribute;
1404  }
1405  else
1406  {
1407  XmlElementAttribute xmlElementAttribute = CreateElementAttribute(typeDesc);
1408  if (typeDesc.IsStructLike)
1409  {
1410  XmlAttributes xmlAttributes2 = new XmlAttributes(xmlReflectionMember.MemberType);
1411  if (xmlAttributes2.XmlRoot != null)
1412  {
1413  if (xmlAttributes2.XmlRoot.ElementName.Length > 0)
1414  {
1415  xmlElementAttribute.ElementName = xmlAttributes2.XmlRoot.ElementName;
1416  }
1417  if (rpc)
1418  {
1419  xmlElementAttribute.Namespace = null;
1420  if (xmlAttributes2.XmlRoot.IsNullableSpecified)
1421  {
1422  xmlElementAttribute.IsNullable = xmlAttributes2.XmlRoot.IsNullable;
1423  }
1424  }
1425  else
1426  {
1427  xmlElementAttribute.Namespace = xmlAttributes2.XmlRoot.Namespace;
1428  xmlElementAttribute.IsNullable = xmlAttributes2.XmlRoot.IsNullable;
1429  }
1430  }
1431  }
1432  if (xmlElementAttribute.ElementName.Length == 0)
1433  {
1434  xmlElementAttribute.ElementName = xmlReflectionMember.MemberName;
1435  }
1436  if (xmlElementAttribute.Namespace == null && !rpc)
1437  {
1438  xmlElementAttribute.Namespace = ns;
1439  }
1440  xmlElementAttribute.Form = form;
1441  xmlAttributes.XmlElements.Add(xmlElementAttribute);
1442  }
1443  }
1444  else if (xmlAttributes.XmlRoot != null)
1445  {
1446  CheckNullable(xmlAttributes.XmlRoot.IsNullable, typeDesc, null);
1447  }
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)
1457  {
1458  choiceIdentifierType = GetChoiceIdentifierType(xmlAttributes.XmlChoiceIdentifier, xmlReflectionMembers, typeDesc.IsArrayLike, fieldModel.Name);
1459  }
1460  ImportAccessorMapping(memberMapping, fieldModel, xmlAttributes, ns, choiceIdentifierType, rpc, openModel, limiter);
1461  if (xmlReflectionMember.OverrideIsNullable && memberMapping.Elements.Length != 0)
1462  {
1463  memberMapping.Elements[0].IsNullable = false;
1464  }
1465  return memberMapping;
1466  }
1467 
1468  internal static XmlReflectionMember FindSpecifiedMember(string memberName, XmlReflectionMember[] reflectionMembers)
1469  {
1470  for (int i = 0; i < reflectionMembers.Length; i++)
1471  {
1472  if (string.Compare(reflectionMembers[i].MemberName, memberName + "Specified", StringComparison.Ordinal) == 0)
1473  {
1474  return reflectionMembers[i];
1475  }
1476  }
1477  return null;
1478  }
1479 
1480  private MemberMapping ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, string ns, RecursionLimiter limiter)
1481  {
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)
1492  {
1493  choiceIdentifierType = GetChoiceIdentifierType(a.XmlChoiceIdentifier, parent, model.FieldTypeDesc.IsArrayLike, model.Name);
1494  }
1495  ImportAccessorMapping(memberMapping, model, a, ns, choiceIdentifierType, rpc: false, openModel: false, limiter);
1496  return memberMapping;
1497  }
1498 
1499  private Type CheckChoiceIdentifierType(Type type, bool isArrayLike, string identifierName, string memberName)
1500  {
1501  if (type.IsArray)
1502  {
1503  if (!isArrayLike)
1504  {
1505  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentifierType", identifierName, memberName, type.GetElementType().FullName));
1506  }
1507  type = type.GetElementType();
1508  }
1509  else if (isArrayLike)
1510  {
1511  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentifierArrayType", identifierName, memberName, type.FullName));
1512  }
1513  if (!type.IsEnum)
1514  {
1515  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentifierTypeEnum", identifierName));
1516  }
1517  return type;
1518  }
1519 
1520  private Type GetChoiceIdentifierType(XmlChoiceIdentifierAttribute choice, XmlReflectionMember[] xmlReflectionMembers, bool isArrayLike, string accessorName)
1521  {
1522  for (int i = 0; i < xmlReflectionMembers.Length; i++)
1523  {
1524  if (choice.MemberName == xmlReflectionMembers[i].MemberName)
1525  {
1526  return CheckChoiceIdentifierType(xmlReflectionMembers[i].MemberType, isArrayLike, choice.MemberName, accessorName);
1527  }
1528  }
1529  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentiferMemberMissing", choice.MemberName, accessorName));
1530  }
1531 
1532  private Type GetChoiceIdentifierType(XmlChoiceIdentifierAttribute choice, StructModel structModel, bool isArrayLike, string accessorName)
1533  {
1534  MemberInfo[] array = structModel.Type.GetMember(choice.MemberName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
1535  if (array == null || array.Length == 0)
1536  {
1537  PropertyInfo property = structModel.Type.GetProperty(choice.MemberName, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
1538  if (property == null)
1539  {
1540  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentiferMemberMissing", choice.MemberName, accessorName));
1541  }
1542  array = new MemberInfo[1]
1543  {
1544  property
1545  };
1546  }
1547  else if (array.Length > 1)
1548  {
1549  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentiferAmbiguous", choice.MemberName));
1550  }
1551  FieldModel fieldModel = structModel.GetFieldModel(array[0]);
1552  if (fieldModel == null)
1553  {
1554  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentiferMemberMissing", choice.MemberName, accessorName));
1555  }
1556  choice.MemberInfo = fieldModel.MemberInfo;
1557  Type fieldType = fieldModel.FieldType;
1558  return CheckChoiceIdentifierType(fieldType, isArrayLike, choice.MemberName, accessorName);
1559  }
1560 
1561  private void CreateArrayElementsFromAttributes(ArrayMapping arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType, string arrayElementNs, RecursionLimiter limiter)
1562  {
1563  NameTable nameTable = new NameTable();
1564  int num = 0;
1565  while (attributes != null && num < attributes.Count)
1566  {
1567  XmlArrayItemAttribute xmlArrayItemAttribute = attributes[num];
1568  if (xmlArrayItemAttribute.NestingLevel == arrayNestingLevel)
1569  {
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);
1581  }
1582  num++;
1583  }
1584  arrayMapping.Elements = (ElementAccessor[])nameTable.ToArray(typeof(ElementAccessor));
1585  }
1586 
1587  private void ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, string ns, Type choiceIdentifierType, bool rpc, bool openModel, RecursionLimiter limiter)
1588  {
1589  XmlSchemaForm xmlSchemaForm = XmlSchemaForm.Qualified;
1590  int num = arrayNestingLevel;
1591  int num2 = -1;
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;
1599  ArrayList arrayList = new ArrayList();
1600  NameTable nameTable = new NameTable();
1601  accessor.TypeDesc = typeScope.GetTypeDesc(fieldType);
1602  XmlAttributeFlags xmlFlags = a.XmlFlags;
1603  accessor.Ignore = a.XmlIgnore;
1604  if (rpc)
1605  {
1606  CheckTopLevelAttributes(a, name);
1607  }
1608  else
1609  {
1610  CheckAmbiguousChoice(a, fieldType, name);
1611  }
1612  XmlAttributeFlags xmlAttributeFlags = (XmlAttributeFlags)1300;
1613  XmlAttributeFlags xmlAttributeFlags2 = (XmlAttributeFlags)544;
1614  XmlAttributeFlags xmlAttributeFlags3 = (XmlAttributeFlags)10;
1615  if ((xmlFlags & xmlAttributeFlags3) != 0 && fieldType == typeof(byte[]))
1616  {
1617  accessor.TypeDesc = typeScope.GetArrayTypeDesc(fieldType);
1618  }
1619  if (a.XmlChoiceIdentifier != null)
1620  {
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);
1626  }
1627  if (accessor.TypeDesc.IsArrayLike)
1628  {
1629  Type arrayElementType = TypeScope.GetArrayElementType(fieldType, model.FieldTypeDesc.FullName + "." + model.Name);
1630  if ((xmlFlags & xmlAttributeFlags2) != 0)
1631  {
1632  if ((xmlFlags & xmlAttributeFlags2) != xmlFlags)
1633  {
1634  throw new InvalidOperationException(Res.GetString("XmlIllegalAttributesArrayAttribute"));
1635  }
1636  if (a.XmlAttribute != null && !accessor.TypeDesc.ArrayElementTypeDesc.IsPrimitive && !accessor.TypeDesc.ArrayElementTypeDesc.IsEnum)
1637  {
1638  if (accessor.TypeDesc.ArrayElementTypeDesc.Kind == TypeKind.Serializable)
1639  {
1640  throw new InvalidOperationException(Res.GetString("XmlIllegalAttrOrTextInterface", name, accessor.TypeDesc.ArrayElementTypeDesc.FullName, typeof(IXmlSerializable).Name));
1641  }
1642  throw new InvalidOperationException(Res.GetString("XmlIllegalAttrOrText", name, accessor.TypeDesc.ArrayElementTypeDesc.FullName));
1643  }
1644  bool flag = a.XmlAttribute != null && (accessor.TypeDesc.ArrayElementTypeDesc.IsPrimitive || accessor.TypeDesc.ArrayElementTypeDesc.IsEnum);
1645  if (a.XmlAnyAttribute != null)
1646  {
1647  a.XmlAttribute = new XmlAttributeAttribute();
1648  }
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)
1656  {
1657  attributeAccessor.Form = XmlSchemaForm.Qualified;
1658  }
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)
1666  {
1667  if (xsdAttributes == null)
1668  {
1669  xsdAttributes = new NameTable();
1670  }
1671  attributeAccessor = (AttributeAccessor)ReconcileAccessor(attributeAccessor, xsdAttributes);
1672  }
1673  accessor.Attribute = attributeAccessor;
1674  }
1675  else if ((xmlFlags & xmlAttributeFlags) != 0)
1676  {
1677  if ((xmlFlags & xmlAttributeFlags) != xmlFlags)
1678  {
1679  throw new InvalidOperationException(Res.GetString("XmlIllegalElementsArrayAttribute"));
1680  }
1681  if (a.XmlText != null)
1682  {
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)))
1689  {
1690  throw new InvalidOperationException(Res.GetString("XmlIllegalArrayTextAttribute", name));
1691  }
1692  accessor.Text = textAccessor;
1693  }
1694  if (a.XmlText == null && a.XmlElements.Count == 0 && a.XmlAnyElements.Count == 0)
1695  {
1696  a.XmlElements.Add(CreateElementAttribute(accessor.TypeDesc));
1697  }
1698  for (int i = 0; i < a.XmlElements.Count; i++)
1699  {
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)
1708  {
1709  elementAccessor.Name = XmlConvert.EncodeLocalName((xmlElementAttribute.ElementName.Length == 0) ? name : xmlElementAttribute.ElementName);
1710  }
1711  else
1712  {
1713  elementAccessor.Name = ((xmlElementAttribute.ElementName.Length == 0) ? elementAccessor.Mapping.DefaultElementName : XmlConvert.EncodeLocalName(xmlElementAttribute.ElementName));
1714  }
1715  elementAccessor.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1716  if (xmlElementAttribute.IsNullableSpecified && !xmlElementAttribute.IsNullable && typeModel.TypeDesc.IsOptionalValue)
1717  {
1718  throw new InvalidOperationException(Res.GetString("XmlInvalidNotNullable", typeModel.TypeDesc.BaseTypeDesc.FullName, "XmlElement"));
1719  }
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);
1723  if (!rpc)
1724  {
1725  CheckForm(elementAccessor.Form, ns != elementAccessor.Namespace);
1726  elementAccessor = ReconcileLocalAccessor(elementAccessor, ns);
1727  }
1728  if (xmlElementAttribute.Order != -1)
1729  {
1730  if (xmlElementAttribute.Order != num2 && num2 != -1)
1731  {
1732  throw new InvalidOperationException(Res.GetString("XmlSequenceMatch", "Order"));
1733  }
1734  num2 = xmlElementAttribute.Order;
1735  }
1736  AddUniqueAccessor(nameTable, elementAccessor);
1737  arrayList.Add(elementAccessor);
1738  }
1739  NameTable nameTable2 = new NameTable();
1740  for (int j = 0; j < a.XmlAnyElements.Count; j++)
1741  {
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))
1745  {
1746  throw new InvalidOperationException(Res.GetString("XmlIllegalAnyElement", arrayElementType.FullName));
1747  }
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)
1751  {
1752  continue;
1753  }
1754  nameTable2[name2, text2] = xmlAnyElementAttribute;
1755  if (nameTable[name2, (text2 == null) ? ns : text2] != null)
1756  {
1757  throw new InvalidOperationException(Res.GetString("XmlAnyElementDuplicate", name, xmlAnyElementAttribute.Name, (xmlAnyElementAttribute.Namespace == null) ? "null" : xmlAnyElementAttribute.Namespace));
1758  }
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)
1767  {
1768  typeModel2.TypeDesc.IsMixed = true;
1769  }
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);
1775  if (!rpc)
1776  {
1777  CheckForm(elementAccessor2.Form, ns != elementAccessor2.Namespace);
1778  elementAccessor2 = ReconcileLocalAccessor(elementAccessor2, ns);
1779  }
1780  nameTable.Add(elementAccessor2.Name, elementAccessor2.Namespace, elementAccessor2);
1781  arrayList.Add(elementAccessor2);
1782  if (xmlAnyElementAttribute.Order != -1)
1783  {
1784  if (xmlAnyElementAttribute.Order != num2 && num2 != -1)
1785  {
1786  throw new InvalidOperationException(Res.GetString("XmlSequenceMatch", "Order"));
1787  }
1788  num2 = xmlAnyElementAttribute.Order;
1789  }
1790  }
1791  }
1792  else
1793  {
1794  if ((xmlFlags & xmlAttributeFlags3) != 0 && (xmlFlags & xmlAttributeFlags3) != xmlFlags)
1795  {
1796  throw new InvalidOperationException(Res.GetString("XmlIllegalArrayArrayAttribute"));
1797  }
1798  TypeDesc typeDesc5 = typeScope.GetTypeDesc(arrayElementType);
1799  if (a.XmlArray == null)
1800  {
1801  a.XmlArray = CreateArrayAttribute(accessor.TypeDesc);
1802  }
1803  if (CountAtLevel(a.XmlArrayItems, arrayNestingLevel) == 0)
1804  {
1805  a.XmlArrayItems.Add(CreateArrayItemAttribute(typeDesc5, arrayNestingLevel));
1806  }
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);
1817  if (!rpc)
1818  {
1819  CheckForm(elementAccessor3.Form, ns != elementAccessor3.Namespace);
1820  elementAccessor3 = ReconcileLocalAccessor(elementAccessor3, ns);
1821  }
1822  savedArrayItemAttributes = null;
1823  savedArrayNamespace = null;
1824  AddUniqueAccessor(nameTable, elementAccessor3);
1825  arrayList.Add(elementAccessor3);
1826  }
1827  }
1828  else if (!accessor.TypeDesc.IsVoid)
1829  {
1830  XmlAttributeFlags xmlAttributeFlags4 = (XmlAttributeFlags)3380;
1831  if ((xmlFlags & xmlAttributeFlags4) != xmlFlags)
1832  {
1833  throw new InvalidOperationException(Res.GetString("XmlIllegalAttribute"));
1834  }
1835  if (accessor.TypeDesc.IsPrimitive || accessor.TypeDesc.IsEnum)
1836  {
1837  if (a.XmlAnyElements.Count > 0)
1838  {
1839  throw new InvalidOperationException(Res.GetString("XmlIllegalAnyElement", accessor.TypeDesc.FullName));
1840  }
1841  if (a.XmlAttribute != null)
1842  {
1843  if (a.XmlElements.Count > 0)
1844  {
1845  throw new InvalidOperationException(Res.GetString("XmlIllegalAttribute"));
1846  }
1847  if (a.XmlAttribute.Type != null)
1848  {
1849  throw new InvalidOperationException(Res.GetString("XmlIllegalType", "XmlAttribute"));
1850  }
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)
1856  {
1857  attributeAccessor2.Form = XmlSchemaForm.Qualified;
1858  }
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)
1865  {
1866  if (xsdAttributes == null)
1867  {
1868  xsdAttributes = new NameTable();
1869  }
1870  attributeAccessor2 = (AttributeAccessor)ReconcileAccessor(attributeAccessor2, xsdAttributes);
1871  }
1872  accessor.Attribute = attributeAccessor2;
1873  }
1874  else
1875  {
1876  if (a.XmlText != null)
1877  {
1878  if (a.XmlText.Type != null && a.XmlText.Type != fieldType)
1879  {
1880  throw new InvalidOperationException(Res.GetString("XmlIllegalType", "XmlText"));
1881  }
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;
1886  }
1887  else if (a.XmlElements.Count == 0)
1888  {
1889  a.XmlElements.Add(CreateElementAttribute(accessor.TypeDesc));
1890  }
1891  for (int k = 0; k < a.XmlElements.Count; k++)
1892  {
1893  XmlElementAttribute xmlElementAttribute2 = a.XmlElements[k];
1894  if (xmlElementAttribute2.Type != null && typeScope.GetTypeDesc(xmlElementAttribute2.Type) != accessor.TypeDesc)
1895  {
1896  throw new InvalidOperationException(Res.GetString("XmlIllegalType", "XmlElement"));
1897  }
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)
1904  {
1905  elementAccessor4.Any = true;
1906  }
1907  elementAccessor4.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1908  if (xmlElementAttribute2.IsNullableSpecified && !xmlElementAttribute2.IsNullable && typeModel3.TypeDesc.IsOptionalValue)
1909  {
1910  throw new InvalidOperationException(Res.GetString("XmlInvalidNotNullable", typeModel3.TypeDesc.BaseTypeDesc.FullName, "XmlElement"));
1911  }
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);
1915  if (!rpc)
1916  {
1917  CheckForm(elementAccessor4.Form, ns != elementAccessor4.Namespace);
1918  elementAccessor4 = ReconcileLocalAccessor(elementAccessor4, ns);
1919  }
1920  if (xmlElementAttribute2.Order != -1)
1921  {
1922  if (xmlElementAttribute2.Order != num2 && num2 != -1)
1923  {
1924  throw new InvalidOperationException(Res.GetString("XmlSequenceMatch", "Order"));
1925  }
1926  num2 = xmlElementAttribute2.Order;
1927  }
1928  AddUniqueAccessor(nameTable, elementAccessor4);
1929  arrayList.Add(elementAccessor4);
1930  }
1931  }
1932  }
1933  else if (a.Xmlns)
1934  {
1935  if (xmlFlags != XmlAttributeFlags.XmlnsDeclarations)
1936  {
1937  throw new InvalidOperationException(Res.GetString("XmlSoleXmlnsAttribute"));
1938  }
1939  if (fieldType != typeof(XmlSerializerNamespaces))
1940  {
1941  throw new InvalidOperationException(Res.GetString("XmlXmlnsInvalidType", name, fieldType.FullName, typeof(XmlSerializerNamespaces).FullName));
1942  }
1943  accessor.Xmlns = new XmlnsAccessor();
1944  accessor.Ignore = true;
1945  }
1946  else
1947  {
1948  if (a.XmlAttribute != null || a.XmlText != null)
1949  {
1950  if (accessor.TypeDesc.Kind == TypeKind.Serializable)
1951  {
1952  throw new InvalidOperationException(Res.GetString("XmlIllegalAttrOrTextInterface", name, accessor.TypeDesc.FullName, typeof(IXmlSerializable).Name));
1953  }
1954  throw new InvalidOperationException(Res.GetString("XmlIllegalAttrOrText", name, accessor.TypeDesc));
1955  }
1956  if (a.XmlElements.Count == 0 && a.XmlAnyElements.Count == 0)
1957  {
1958  a.XmlElements.Add(CreateElementAttribute(accessor.TypeDesc));
1959  }
1960  for (int l = 0; l < a.XmlElements.Count; l++)
1961  {
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)
1970  {
1971  elementAccessor5.Name = XmlConvert.EncodeLocalName((xmlElementAttribute3.ElementName.Length == 0) ? name : xmlElementAttribute3.ElementName);
1972  }
1973  else
1974  {
1975  elementAccessor5.Name = ((xmlElementAttribute3.ElementName.Length == 0) ? elementAccessor5.Mapping.DefaultElementName : XmlConvert.EncodeLocalName(xmlElementAttribute3.ElementName));
1976  }
1977  elementAccessor5.Default = GetDefaultValue(model.FieldTypeDesc, model.FieldType, a);
1978  if (xmlElementAttribute3.IsNullableSpecified && !xmlElementAttribute3.IsNullable && typeModel4.TypeDesc.IsOptionalValue)
1979  {
1980  throw new InvalidOperationException(Res.GetString("XmlInvalidNotNullable", typeModel4.TypeDesc.BaseTypeDesc.FullName, "XmlElement"));
1981  }
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);
1985  if (!rpc)
1986  {
1987  CheckForm(elementAccessor5.Form, ns != elementAccessor5.Namespace);
1988  elementAccessor5 = ReconcileLocalAccessor(elementAccessor5, ns);
1989  }
1990  if (xmlElementAttribute3.Order != -1)
1991  {
1992  if (xmlElementAttribute3.Order != num2 && num2 != -1)
1993  {
1994  throw new InvalidOperationException(Res.GetString("XmlSequenceMatch", "Order"));
1995  }
1996  num2 = xmlElementAttribute3.Order;
1997  }
1998  AddUniqueAccessor(nameTable, elementAccessor5);
1999  arrayList.Add(elementAccessor5);
2000  }
2001  NameTable nameTable3 = new NameTable();
2002  for (int m = 0; m < a.XmlAnyElements.Count; m++)
2003  {
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))
2007  {
2008  throw new InvalidOperationException(Res.GetString("XmlIllegalAnyElement", fieldType.FullName));
2009  }
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)
2013  {
2014  continue;
2015  }
2016  nameTable3[name3, text3] = xmlAnyElementAttribute2;
2017  if (nameTable[name3, (text3 == null) ? ns : text3] != null)
2018  {
2019  throw new InvalidOperationException(Res.GetString("XmlAnyElementDuplicate", name, xmlAnyElementAttribute2.Name, (xmlAnyElementAttribute2.Namespace == null) ? "null" : xmlAnyElementAttribute2.Namespace));
2020  }
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)
2029  {
2030  typeModel5.TypeDesc.IsMixed = true;
2031  }
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);
2037  if (!rpc)
2038  {
2039  CheckForm(elementAccessor6.Form, ns != elementAccessor6.Namespace);
2040  elementAccessor6 = ReconcileLocalAccessor(elementAccessor6, ns);
2041  }
2042  if (xmlAnyElementAttribute2.Order != -1)
2043  {
2044  if (xmlAnyElementAttribute2.Order != num2 && num2 != -1)
2045  {
2046  throw new InvalidOperationException(Res.GetString("XmlSequenceMatch", "Order"));
2047  }
2048  num2 = xmlAnyElementAttribute2.Order;
2049  }
2050  nameTable.Add(elementAccessor6.Name, elementAccessor6.Namespace, elementAccessor6);
2051  arrayList.Add(elementAccessor6);
2052  }
2053  }
2054  }
2055  accessor.Elements = (ElementAccessor[])arrayList.ToArray(typeof(ElementAccessor));
2056  accessor.SequenceId = num2;
2057  if (rpc)
2058  {
2059  if (accessor.TypeDesc.IsArrayLike && accessor.Elements.Length != 0 && !(accessor.Elements[0].Mapping is ArrayMapping))
2060  {
2061  throw new InvalidOperationException(Res.GetString("XmlRpcLitArrayElement", accessor.Elements[0].Name));
2062  }
2063  if (accessor.Xmlns != null)
2064  {
2065  throw new InvalidOperationException(Res.GetString("XmlRpcLitXmlns", accessor.Name));
2066  }
2067  }
2068  if (accessor.ChoiceIdentifier != null)
2069  {
2070  accessor.ChoiceIdentifier.MemberIds = new string[accessor.Elements.Length];
2071  for (int n = 0; n < accessor.Elements.Length; n++)
2072  {
2073  bool flag2 = false;
2074  ElementAccessor elementAccessor7 = accessor.Elements[n];
2075  EnumMapping enumMapping = (EnumMapping)accessor.ChoiceIdentifier.Mapping;
2076  for (int num3 = 0; num3 < enumMapping.Constants.Length; num3++)
2077  {
2078  string xmlName = enumMapping.Constants[num3].XmlName;
2079  if (elementAccessor7.Any && elementAccessor7.Name.Length == 0)
2080  {
2081  string b = (elementAccessor7.AnyNamespaces == null) ? "##any" : elementAccessor7.AnyNamespaces;
2082  if (xmlName.Substring(0, xmlName.Length - 1) == b)
2083  {
2084  accessor.ChoiceIdentifier.MemberIds[n] = enumMapping.Constants[num3].Name;
2085  flag2 = true;
2086  break;
2087  }
2088  continue;
2089  }
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))
2094  {
2095  accessor.ChoiceIdentifier.MemberIds[n] = enumMapping.Constants[num3].Name;
2096  flag2 = true;
2097  break;
2098  }
2099  }
2100  if (!flag2)
2101  {
2102  if (elementAccessor7.Any && elementAccessor7.Name.Length == 0)
2103  {
2104  throw new InvalidOperationException(Res.GetString("XmlChoiceMissingAnyValue", accessor.ChoiceIdentifier.Mapping.TypeDesc.FullName));
2105  }
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));
2108  }
2109  }
2110  }
2111  arrayNestingLevel = num;
2112  savedArrayItemAttributes = xmlArrayItemAttributes;
2113  savedArrayNamespace = text;
2114  }
2115 
2116  private void CheckTopLevelAttributes(XmlAttributes a, string accessorName)
2117  {
2118  XmlAttributeFlags xmlFlags = a.XmlFlags;
2119  if ((xmlFlags & (XmlAttributeFlags)544) != 0)
2120  {
2121  throw new InvalidOperationException(Res.GetString("XmlRpcLitAttributeAttributes"));
2122  }
2123  if ((xmlFlags & (XmlAttributeFlags)1284) != 0)
2124  {
2125  throw new InvalidOperationException(Res.GetString("XmlRpcLitAttributes"));
2126  }
2127  if (a.XmlElements != null && a.XmlElements.Count > 0)
2128  {
2129  if (a.XmlElements.Count > 1)
2130  {
2131  throw new InvalidOperationException(Res.GetString("XmlRpcLitElements"));
2132  }
2133  XmlElementAttribute xmlElementAttribute = a.XmlElements[0];
2134  if (xmlElementAttribute.Namespace != null)
2135  {
2136  throw new InvalidOperationException(Res.GetString("XmlRpcLitElementNamespace", "Namespace", xmlElementAttribute.Namespace));
2137  }
2138  if (xmlElementAttribute.IsNullable)
2139  {
2140  throw new InvalidOperationException(Res.GetString("XmlRpcLitElementNullable", "IsNullable", "true"));
2141  }
2142  }
2143  if (a.XmlArray != null && a.XmlArray.Namespace != null)
2144  {
2145  throw new InvalidOperationException(Res.GetString("XmlRpcLitElementNamespace", "Namespace", a.XmlArray.Namespace));
2146  }
2147  }
2148 
2149  private void CheckAmbiguousChoice(XmlAttributes a, Type accessorType, string accessorName)
2150  {
2151  Hashtable hashtable = new Hashtable();
2152  XmlElementAttributes xmlElements = a.XmlElements;
2153  if (xmlElements != null && xmlElements.Count >= 2 && a.XmlChoiceIdentifier == null)
2154  {
2155  for (int i = 0; i < xmlElements.Count; i++)
2156  {
2157  Type key = (xmlElements[i].Type == null) ? accessorType : xmlElements[i].Type;
2158  if (hashtable.Contains(key))
2159  {
2160  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentiferMissing", typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
2161  }
2162  hashtable.Add(key, false);
2163  }
2164  }
2165  if (hashtable.Contains(typeof(XmlElement)) && a.XmlAnyElements.Count > 0)
2166  {
2167  throw new InvalidOperationException(Res.GetString("XmlChoiceIdentiferMissing", typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
2168  }
2169  XmlArrayItemAttributes xmlArrayItems = a.XmlArrayItems;
2170  if (xmlArrayItems == null || xmlArrayItems.Count < 2)
2171  {
2172  return;
2173  }
2174  NameTable nameTable = new NameTable();
2175  int num = 0;
2176  XmlArrayItemAttribute xmlArrayItemAttribute;
2177  while (true)
2178  {
2179  if (num < xmlArrayItems.Count)
2180  {
2181  Type type = (xmlArrayItems[num].Type == null) ? accessorType : xmlArrayItems[num].Type;
2182  string ns = xmlArrayItems[num].NestingLevel.ToString(CultureInfo.InvariantCulture);
2183  xmlArrayItemAttribute = (XmlArrayItemAttribute)nameTable[type.FullName, ns];
2184  if (xmlArrayItemAttribute != null)
2185  {
2186  break;
2187  }
2188  nameTable[type.FullName, ns] = xmlArrayItems[num];
2189  num++;
2190  continue;
2191  }
2192  return;
2193  }
2194  throw new InvalidOperationException(Res.GetString("XmlArrayItemAmbiguousTypes", accessorName, xmlArrayItemAttribute.ElementName, xmlArrayItems[num].ElementName, typeof(XmlElementAttribute).Name, typeof(XmlChoiceIdentifierAttribute).Name, accessorName));
2195  }
2196 
2197  private void CheckChoiceIdentifierMapping(EnumMapping choiceMapping)
2198  {
2199  NameTable nameTable = new NameTable();
2200  int num = 0;
2201  string xmlName;
2202  while (true)
2203  {
2204  if (num < choiceMapping.Constants.Length)
2205  {
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)
2211  {
2212  break;
2213  }
2214  nameTable.Add(name, ns, choiceMapping.Constants[num]);
2215  num++;
2216  continue;
2217  }
2218  return;
2219  }
2220  throw new InvalidOperationException(Res.GetString("XmlChoiceIdDuplicate", choiceMapping.TypeName, xmlName));
2221  }
2222 
2223  private object GetDefaultValue(TypeDesc fieldTypeDesc, Type t, XmlAttributes a)
2224  {
2225  if (a.XmlDefaultValue == null || a.XmlDefaultValue == DBNull.Value)
2226  {
2227  return null;
2228  }
2229  if (fieldTypeDesc.Kind != TypeKind.Primitive && fieldTypeDesc.Kind != TypeKind.Enum)
2230  {
2231  a.XmlDefaultValue = null;
2232  return a.XmlDefaultValue;
2233  }
2234  if (fieldTypeDesc.Kind == TypeKind.Enum)
2235  {
2236  string text = Enum.Format(t, a.XmlDefaultValue, "G").Replace(",", " ");
2237  string b = Enum.Format(t, a.XmlDefaultValue, "D");
2238  if (text == b)
2239  {
2240  throw new InvalidOperationException(Res.GetString("XmlInvalidDefaultValue", text, a.XmlDefaultValue.GetType().FullName));
2241  }
2242  return text;
2243  }
2244  return a.XmlDefaultValue;
2245  }
2246 
2247  private static XmlArrayItemAttribute CreateArrayItemAttribute(TypeDesc typeDesc, int nestingLevel)
2248  {
2249  XmlArrayItemAttribute xmlArrayItemAttribute = new XmlArrayItemAttribute();
2250  xmlArrayItemAttribute.NestingLevel = nestingLevel;
2251  return xmlArrayItemAttribute;
2252  }
2253 
2254  private static XmlArrayAttribute CreateArrayAttribute(TypeDesc typeDesc)
2255  {
2256  return new XmlArrayAttribute();
2257  }
2258 
2259  private static XmlElementAttribute CreateElementAttribute(TypeDesc typeDesc)
2260  {
2261  XmlElementAttribute xmlElementAttribute = new XmlElementAttribute();
2262  xmlElementAttribute.IsNullable = typeDesc.IsOptionalValue;
2263  return xmlElementAttribute;
2264  }
2265 
2266  private static void AddUniqueAccessor(INameScope scope, Accessor accessor)
2267  {
2268  Accessor accessor2 = (Accessor)scope[accessor.Name, accessor.Namespace];
2269  if (accessor2 != null)
2270  {
2271  if (accessor is ElementAccessor)
2272  {
2273  throw new InvalidOperationException(Res.GetString("XmlDuplicateElementName", accessor2.Name, accessor2.Namespace));
2274  }
2275  throw new InvalidOperationException(Res.GetString("XmlDuplicateAttributeName", accessor2.Name, accessor2.Namespace));
2276  }
2277  scope[accessor.Name, accessor.Namespace] = accessor;
2278  }
2279 
2280  private static void AddUniqueAccessor(MemberMapping member, INameScope elements, INameScope attributes, bool isSequence)
2281  {
2282  if (member.Attribute != null)
2283  {
2284  AddUniqueAccessor(attributes, member.Attribute);
2285  }
2286  else if (!isSequence && member.Elements != null && member.Elements.Length != 0)
2287  {
2288  for (int i = 0; i < member.Elements.Length; i++)
2289  {
2290  AddUniqueAccessor(elements, member.Elements[i]);
2291  }
2292  }
2293  }
2294 
2295  private static void CheckForm(XmlSchemaForm form, bool isQualified)
2296  {
2297  if (isQualified && form == XmlSchemaForm.Unqualified)
2298  {
2299  throw new InvalidOperationException(Res.GetString("XmlInvalidFormUnqualified"));
2300  }
2301  }
2302 
2303  private static void CheckNullable(bool isNullable, TypeDesc typeDesc, TypeMapping mapping)
2304  {
2305  if (mapping is NullableMapping || mapping is SerializableMapping || !isNullable || typeDesc.IsNullable)
2306  {
2307  return;
2308  }
2309  throw new InvalidOperationException(Res.GetString("XmlInvalidIsNullable", typeDesc.FullName));
2310  }
2311 
2312  private static ElementAccessor CreateElementAccessor(TypeMapping mapping, string ns)
2313  {
2314  ElementAccessor elementAccessor = new ElementAccessor();
2315  bool flag = mapping.TypeDesc.Kind == TypeKind.Node;
2316  if (!flag && mapping is SerializableMapping)
2317  {
2318  flag = ((SerializableMapping)mapping).IsAny;
2319  }
2320  if (flag)
2321  {
2322  elementAccessor.Any = true;
2323  }
2324  else
2325  {
2326  elementAccessor.Name = mapping.DefaultElementName;
2327  elementAccessor.Namespace = ns;
2328  }
2329  elementAccessor.Mapping = mapping;
2330  return elementAccessor;
2331  }
2332 
2333  internal static XmlTypeMapping GetTopLevelMapping(Type type, string defaultNamespace)
2334  {
2335  XmlAttributes xmlAttributes = new XmlAttributes(type);
2336  TypeDesc typeDesc = new TypeScope().GetTypeDesc(type);
2337  ElementAccessor elementAccessor = new ElementAccessor();
2338  if (typeDesc.Kind == TypeKind.Node)
2339  {
2340  elementAccessor.Any = true;
2341  }
2342  else
2343  {
2344  string @namespace = (xmlAttributes.XmlRoot == null) ? defaultNamespace : xmlAttributes.XmlRoot.Namespace;
2345  string text = string.Empty;
2346  if (xmlAttributes.XmlType != null)
2347  {
2348  text = xmlAttributes.XmlType.TypeName;
2349  }
2350  if (text.Length == 0)
2351  {
2352  text = type.Name;
2353  }
2354  elementAccessor.Name = XmlConvert.EncodeLocalName(text);
2355  elementAccessor.Namespace = @namespace;
2356  }
2357  XmlTypeMapping xmlTypeMapping = new XmlTypeMapping(null, elementAccessor);
2358  xmlTypeMapping.SetKeyInternal(XmlMapping.GenerateKey(type, xmlAttributes.XmlRoot, defaultNamespace));
2359  return xmlTypeMapping;
2360  }
2361  }
2362 }
Obtains information about the attributes of a member and provides access to member metadata.
Definition: MemberInfo.cs:14
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
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.
Definition: XmlSchemaType.cs:7
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.
Definition: Hashtable.cs:916
abstract Type DeclaringType
Gets the class that declares this member.
Definition: MemberInfo.cs:36
Discovers the attributes of a method and provides access to method metadata.
Definition: MethodInfo.cs:13
abstract string FullName
Gets the fully qualified name of the type, including its namespace but not its assembly.
Definition: Type.cs:153
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.
Definition: XmlSchemaForm.cs:7
Represents the base class for custom attributes.
Definition: Attribute.cs:15
XmlSeverityType
Represents the severity of the validation event.
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
BindingFlags
Specifies flags that control binding and the way in which the search for members and types is conduct...
Definition: BindingFlags.cs:10
Definition: __Canon.cs:3
virtual Type ReturnType
Gets the return type of this method.
Definition: MethodInfo.cs:23
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.
Definition: ImportContext.cs:7
static string EncodeLocalName(string name)
Converts the name to a valid XML local name.
Definition: XmlConvert.cs:72
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.
Definition: XmlMapping.cs:5
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
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,...
Definition: Type.cs:18
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.
Definition: MemberInfo.cs:27
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....
Definition: XmlAttributes.cs:8
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
Discovers the attributes of a property and provides access to property metadata.
Definition: PropertyInfo.cs:15
Attribute can be applied to an enumeration.
virtual bool Contains(object key)
Determines whether the T:System.Collections.Hashtable contains a specific key.
Definition: Hashtable.cs:972
XmlSchemaObjectTable SchemaTypes
Gets the post-schema-compilation value of all schema types in the schema.
Definition: XmlSchema.cs:297
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...
Definition: XmlSchema.cs:13
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 ...
Definition: MemberInfo.cs:19
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
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....
Definition: MemberTypes.cs:9
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.
Definition: ArrayList.cs:3083
Encodes and decodes XML names, and provides methods for converting between common language runtime ty...
Definition: XmlConvert.cs:11
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14
Contains a cache of XML Schema definition language (XSD) schemas.
Definition: XmlSchemaSet.cs:8
virtual void Sort()
Sorts the elements in the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:3006