public TypeData(Type type, string elementName, bool isPrimitive, TypeData mappedType, XmlSchemaPatternFacet facet) { #if NET_2_0 if (type.IsGenericTypeDefinition) { throw new InvalidOperationException("Generic type definition cannot be used in serialization. Only specific generic types can be used."); } #endif this.mappedType = mappedType; this.facet = facet; this.type = type; this.typeName = type.Name; this.fullTypeName = type.FullName.Replace('+', '.'); if (isPrimitive) { sType = SchemaTypes.Primitive; } else { if (type.IsEnum) { sType = SchemaTypes.Enum; } else if (typeof(IXmlSerializable).IsAssignableFrom(type)) { sType = SchemaTypes.XmlSerializable; } #if !MOONLIGHT else if (typeof(System.Xml.XmlNode).IsAssignableFrom(type)) { sType = SchemaTypes.XmlNode; } #endif else if (type.IsArray || typeof(IEnumerable).IsAssignableFrom(type)) { sType = SchemaTypes.Array; } else { sType = SchemaTypes.Class; } } if (IsListType) { this.elementName = TypeTranslator.GetArrayName(ListItemTypeData.XmlType); } else { this.elementName = elementName; } if (sType == SchemaTypes.Array || sType == SchemaTypes.Class) { hasPublicConstructor = !type.IsInterface && (type.IsArray || type.GetConstructor(Type.EmptyTypes) != null || type.IsAbstract || type.IsValueType); } }
public string GetSchemaArrayName() { XmlTypeMapElementInfo xmlTypeMapElementInfo = (XmlTypeMapElementInfo)this._itemInfo[0]; if (xmlTypeMapElementInfo.MappedType != null) { return(TypeTranslator.GetArrayName(xmlTypeMapElementInfo.MappedType.XmlType)); } return(TypeTranslator.GetArrayName(xmlTypeMapElementInfo.TypeData.XmlType)); }
public static string GetArrayName(string elemName, int dimensions) { string text = TypeTranslator.GetArrayName(elemName); while (dimensions > 1) { text = "ArrayOf" + text; dimensions--; } return(text); }
public static TypeData GetTypeData(Type runtimeType, string xmlDataType) { Type type = runtimeType; bool flag = false; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { flag = true; type = type.GetGenericArguments()[0]; TypeData typeData = TypeTranslator.GetTypeData(type); if (typeData != null) { TypeData typeData2 = (TypeData)TypeTranslator.nullableTypes[typeData.XmlType]; if (typeData2 == null) { typeData2 = new TypeData(type, typeData.XmlType, false); typeData2.IsNullable = true; TypeTranslator.nullableTypes[typeData.XmlType] = typeData2; } return(typeData2); } } if (xmlDataType != null && xmlDataType.Length != 0) { TypeData primitiveTypeData = TypeTranslator.GetPrimitiveTypeData(xmlDataType); if (!type.IsArray || type == primitiveTypeData.Type) { return(primitiveTypeData); } TypeData typeData3 = (TypeData)TypeTranslator.primitiveArrayTypes[xmlDataType]; if (typeData3 != null) { return(typeData3); } if (primitiveTypeData.Type == type.GetElementType()) { typeData3 = new TypeData(type, TypeTranslator.GetArrayName(primitiveTypeData.XmlType), false); TypeTranslator.primitiveArrayTypes[xmlDataType] = typeData3; return(typeData3); } throw new InvalidOperationException(string.Concat(new object[] { "Cannot convert values of type '", type.GetElementType(), "' to '", xmlDataType, "'" })); } else { TypeData typeData4 = TypeTranslator.nameCache[runtimeType] as TypeData; if (typeData4 != null) { return(typeData4); } string text; if (type.IsArray) { string xmlType = TypeTranslator.GetTypeData(type.GetElementType()).XmlType; text = TypeTranslator.GetArrayName(xmlType); } else if (type.IsGenericType && !type.IsGenericTypeDefinition) { text = XmlConvert.EncodeLocalName(type.Name.Substring(0, type.Name.IndexOf('`'))) + "Of"; foreach (Type type2 in type.GetGenericArguments()) { text += ((!type2.IsArray && !type2.IsGenericType) ? CodeIdentifier.MakePascal(XmlConvert.EncodeLocalName(type2.Name)) : TypeTranslator.GetTypeData(type2).XmlType); } } else { text = XmlConvert.EncodeLocalName(type.Name); } typeData4 = new TypeData(type, text, false); if (flag) { typeData4.IsNullable = true; } TypeTranslator.nameCache[runtimeType] = typeData4; return(typeData4); } }
XmlTypeMapping ImportListMapping(Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel) { TypeData typeData = TypeTranslator.GetTypeData(type); ListMap obmap = new ListMap(); if (!allowPrivateTypes) { ReflectionHelper.CheckSerializableType(type); } if (atts == null) { atts = new XmlAttributes(); } Type itemType = typeData.ListItemType; // warning: byte[][] should not be considered multiarray bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray); XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList(); foreach (XmlArrayItemAttribute att in atts.XmlArrayItems) { if (att.NestingLevel != nestingLevel) { continue; } Type elemType = (att.Type != null) ? att.Type : itemType; XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, TypeTranslator.GetTypeData(elemType, att.DataType)); elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace; if (elem.Namespace == null) { elem.Namespace = ""; } elem.Form = att.Form; elem.IsNullable = att.IsNullable && CanBeNull(elem.TypeData); elem.NestingLevel = att.NestingLevel; if (isMultiArray) { elem.MappedType = ImportListMapping(elemType, null, elem.Namespace, atts, nestingLevel + 1); } else if (elem.TypeData.IsComplexType) { elem.MappedType = ImportTypeMapping(elemType, null, elem.Namespace); } if (att.ElementName != null) { elem.ElementName = att.ElementName; } else if (elem.MappedType != null) { elem.ElementName = elem.MappedType.ElementName; } else { elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType; } list.Add(elem); } if (list.Count == 0) { XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, TypeTranslator.GetTypeData(itemType)); if (isMultiArray) { elem.MappedType = ImportListMapping(itemType, null, defaultNamespace, atts, nestingLevel + 1); } else if (elem.TypeData.IsComplexType) { elem.MappedType = ImportTypeMapping(itemType, null, defaultNamespace); } if (elem.MappedType != null) { elem.ElementName = elem.MappedType.XmlType; } else { elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType; } elem.Namespace = (defaultNamespace != null) ? defaultNamespace : ""; elem.IsNullable = CanBeNull(elem.TypeData); list.Add(elem); } obmap.ItemInfo = list; // If there can be different element names (types) in the array, then its name cannot // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN string baseName; if (list.Count > 1) { baseName = "ArrayOfChoice" + (arrayChoiceCount++); } else { XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]); if (elem.MappedType != null) { baseName = TypeTranslator.GetArrayName(elem.MappedType.XmlType); } else { baseName = TypeTranslator.GetArrayName(elem.ElementName); } } // Avoid name colisions int nameCount = 1; string name = baseName; do { XmlTypeMapping foundMap = helper.GetRegisteredSchemaType(name, defaultNamespace); if (foundMap == null) { nameCount = -1; } else if (obmap.Equals(foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) { return(foundMap); } else { name = baseName + (nameCount++); } }while (nameCount != -1); XmlTypeMapping map = CreateTypeMapping(typeData, root, name, defaultNamespace); map.ObjectMap = obmap; // Register any of the including types as a derived class of object XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes(typeof(XmlIncludeAttribute), false); XmlTypeMapping objectMapping = ImportTypeMapping(typeof(object)); for (int i = 0; i < includes.Length; i++) { Type includedType = includes[i].Type; objectMapping.DerivedTypes.Add(ImportTypeMapping(includedType, null, defaultNamespace)); } // Register this map as a derived class of object helper.RegisterSchemaType(map, name, defaultNamespace); ImportTypeMapping(typeof(object)).DerivedTypes.Add(map); return(map); }