XmlTypeMapElementInfoList ImportAnyElementInfo(string defaultNamespace, XmlReflectionMember rmember, XmlTypeMapMemberElement member, XmlAttributes atts) { XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList(); ImportTextElementInfo(list, rmember.MemberType, member, atts); foreach (XmlAnyElementAttribute att in atts.XmlAnyElements) { XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, TypeTranslator.GetTypeData(typeof(XmlElement))); if (att.Name != null && att.Name != string.Empty) { elem.ElementName = att.Name; elem.Namespace = (att.Namespace != null) ? att.Namespace : ""; } else { elem.IsUnnamedAnyElement = true; elem.Namespace = defaultNamespace; if (att.Namespace != null) { throw new InvalidOperationException("The element " + rmember.MemberName + " has been attributed with an XmlAnyElementAttribute and a namespace '" + att.Namespace + "', but no name. When a namespace is supplied, a name is also required. Supply a name or remove the namespace."); } } list.Add(elem); } return(list); }
void ImportTextElementInfo(XmlTypeMapElementInfoList list, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts) { if (atts.XmlText != null) { member.IsXmlTextCollector = true; if (atts.XmlText.Type != null) { defaultType = atts.XmlText.Type; } if (defaultType == typeof(XmlNode)) { defaultType = typeof(XmlText); // Nodes must be text nodes } XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType)); if (elem.TypeData.SchemaType != SchemaTypes.Primitive && elem.TypeData.SchemaType != SchemaTypes.Enum && elem.TypeData.SchemaType != SchemaTypes.XmlNode && !(elem.TypeData.SchemaType == SchemaTypes.Array && elem.TypeData.ListItemTypeData.SchemaType == SchemaTypes.XmlNode) ) { throw new InvalidOperationException("XmlText cannot be used to encode complex types"); } elem.IsTextElement = true; elem.WrappedElement = false; list.Add(elem); } }
protected void WriteReferencedElements() { if (referencedElements == null) { return; } if (callbacks == null) { return; } while (referencedElements.Count > 0) { object o = referencedElements.Dequeue(); TypeData td = TypeTranslator.GetTypeData(o.GetType()); WriteCallbackInfo info = (WriteCallbackInfo)callbacks[o.GetType()]; WriteStartElement(info.TypeName, info.TypeNs, true); Writer.WriteAttributeString("id", GetId(o, false)); if (td.SchemaType != SchemaTypes.Array) // Array use its own "arrayType" attribute { WriteXsiType(info.TypeName, info.TypeNs); } info.Callback(o); WriteEndElement(); } }
internal void AddKeyHash(StringBuilder sb) { sb.Append("SA "); if (this.soapIgnore) { sb.Append('i'); } if (this.soapAttribute != null) { this.soapAttribute.AddKeyHash(sb); } if (this.soapElement != null) { this.soapElement.AddKeyHash(sb); } if (this.soapEnum != null) { this.soapEnum.AddKeyHash(sb); } if (this.soapType != null) { this.soapType.AddKeyHash(sb); } if (this.soapDefaultValue == null) { sb.Append("n"); } else if (!(this.soapDefaultValue is DBNull)) { string str = XmlCustomFormatter.ToXmlString(TypeTranslator.GetTypeData(this.soapDefaultValue.GetType()), this.soapDefaultValue); sb.Append("v" + str); } sb.Append("|"); }
protected void WriteTypedPrimitive(string name, string ns, object o, bool xsiType) { string value; TypeData td = TypeTranslator.GetTypeData(o.GetType()); name = XmlCustomFormatter.FromXmlName(name); Writer.WriteStartElement(name, ns); if (o is XmlQualifiedName) { value = FromXmlQualifiedName((XmlQualifiedName)o); } else { value = XmlCustomFormatter.ToXmlString(td, o); } if (xsiType) { if (td.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(string.Format(unexpectedTypeError, o.GetType().FullName)); } WriteXsiType(td.XmlType, XmlSchema.Namespace); } WriteValue(value); Writer.WriteEndElement(); }
XmlTypeMapping ImportEnumMapping(Type type, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, defaultNamespace)); if (map != null) { return(map); } map = CreateTypeMapping(typeData, null, defaultNamespace); helper.RegisterClrType(map, type, map.Namespace); map.MultiReferenceType = true; string [] names = Enum.GetNames(type); EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember[names.Length]; for (int n = 0; n < names.Length; n++) { MemberInfo[] mem = type.GetMember(names[n]); string xmlName = names[n]; object[] atts = mem[0].GetCustomAttributes(typeof(SoapEnumAttribute), false); if (atts.Length > 0) { xmlName = ((SoapEnumAttribute)atts[0]).Name; } members[n] = new EnumMap.EnumMapMember(xmlName, names[n]); } bool isFlags = type.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0; map.ObjectMap = new EnumMap(members, isFlags); ImportTypeMapping(typeof(object), defaultNamespace).DerivedTypes.Add(map); return(map); }
public static void CheckSerializableType(Type type, bool allowPrivateConstructors) { if (type.IsArray) { return; } if (!allowPrivateConstructors && type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, empty_modifiers) == null && !type.IsAbstract && !type.IsValueType) { throw new InvalidOperationException(type.FullName + " cannot be serialized because it does not have a default public constructor"); } if (type.IsInterface && !TypeTranslator.GetTypeData(type).IsListType) { throw new InvalidOperationException(type.FullName + " cannot be serialized because it is an interface"); } Type t = type; Type oldt = null; do { if (!t.IsPublic && !t.IsNestedPublic) { throw new InvalidOperationException(type.FullName + " is inaccessible due to its protection level. Only public types can be processed"); } oldt = t; t = t.DeclaringType; }while (t != null && t != oldt); }
internal void AddKeyHash(System.Text.StringBuilder sb) { sb.Append("XA "); KeyHelper.AddField(sb, 1, xmlIgnore); KeyHelper.AddField(sb, 2, xmlns); KeyHelper.AddField(sb, 3, xmlAnyAttribute != null); xmlAnyElements.AddKeyHash(sb); xmlArrayItems.AddKeyHash(sb); xmlElements.AddKeyHash(sb); if (xmlArray != null) { xmlArray.AddKeyHash(sb); } if (xmlAttribute != null) { xmlAttribute.AddKeyHash(sb); } if (xmlDefaultValue == null) { sb.Append("n"); } else if (!(xmlDefaultValue is System.DBNull)) { string v = XmlCustomFormatter.ToXmlString(TypeTranslator.GetTypeData(xmlDefaultValue.GetType()), xmlDefaultValue); sb.Append("v" + v); } if (xmlEnum != null) { xmlEnum.AddKeyHash(sb); } if (xmlRoot != null) { xmlRoot.AddKeyHash(sb); } if (xmlText != null) { xmlText.AddKeyHash(sb); } if (xmlType != null) { xmlType.AddKeyHash(sb); } if (xmlChoiceIdentifier != null) { xmlChoiceIdentifier.AddKeyHash(sb); } sb.Append("|"); }
XmlSchemaSimpleType GetSchemaSimpleListType(TypeData typeData) { XmlSchemaSimpleType stype = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList(); TypeData itemTypeData = TypeTranslator.GetTypeData(typeData.ListItemType); list.ItemTypeName = new XmlQualifiedName(itemTypeData.XmlType, XmlSchema.Namespace); stype.Content = list; return(stype); }
private XmlSchemaSimpleType GetSchemaSimpleListType(TypeData typeData) { XmlSchemaSimpleType xmlSchemaSimpleType = new XmlSchemaSimpleType(); XmlSchemaSimpleTypeList xmlSchemaSimpleTypeList = new XmlSchemaSimpleTypeList(); TypeData typeData2 = TypeTranslator.GetTypeData(typeData.ListItemType); xmlSchemaSimpleTypeList.ItemTypeName = new XmlQualifiedName(typeData2.XmlType, "http://www.w3.org/2001/XMLSchema"); xmlSchemaSimpleType.Content = xmlSchemaSimpleTypeList; return(xmlSchemaSimpleType); }
protected void WritePotentiallyReferencingElement(string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable) { if (o == null) { if (isNullable) { WriteNullTagEncoded(n, ns); } return; } WriteStartElement(n, ns, true); CheckReferenceQueue(); if (callbacks.ContainsKey(o.GetType())) { WriteCallbackInfo info = (WriteCallbackInfo)callbacks[o.GetType()]; if (o.GetType().IsEnum) { info.Callback(o); } else if (suppressReference) { Writer.WriteAttributeString("id", GetId(o, false)); if (ambientType != o.GetType()) { WriteXsiType(info.TypeName, info.TypeNs); } info.Callback(o); } else { if (!AlreadyQueued(o)) { referencedElements.Enqueue(o); } Writer.WriteAttributeString("href", "#" + GetId(o, true)); } } else { // Must be a primitive type TypeData td = TypeTranslator.GetTypeData(o.GetType()); if (td.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException("Invalid type: " + o.GetType().FullName); } WriteXsiType(td.XmlType, XmlSchema.Namespace); Writer.WriteString(XmlCustomFormatter.ToXmlString(td, o)); } WriteEndElement(); }
public static TypeData GetDefaultPrimitiveTypeData(TypeData primType) { if (primType.SchemaType == SchemaTypes.Primitive) { TypeData typeData = TypeTranslator.GetTypeData(primType.Type, null); if (typeData != primType) { return(typeData); } } return(primType); }
/// <summary>Generates a mapping to an XML Schema element for a .NET Framework type.</summary> /// <returns>Internal .NET Framework mapping of a type to an XML Schema element.</returns> /// <param name="type">The .NET Framework type for which to generate a type mapping. </param> /// <param name="defaultNamespace">The default XML namespace to use.</param> public XmlTypeMapping ImportTypeMapping(Type type, string defaultNamespace) { if (type == null) { throw new ArgumentNullException("type"); } if (type == typeof(void)) { throw new InvalidOperationException("Type " + type.Name + " may not be serialized."); } return(this.ImportTypeMapping(TypeTranslator.GetTypeData(type), defaultNamespace)); }
XmlTypeMapping ImportPrimitiveMapping(Type type, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, defaultNamespace)); if (map != null) { return(map); } map = CreateTypeMapping(typeData, null, defaultNamespace); helper.RegisterClrType(map, type, map.Namespace); return(map); }
XmlTypeMapping ImportXmlSerializableMapping(Type type, XmlRootAttribute root, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, root, defaultNamespace)); if (map != null) { return(map); } map = CreateTypeMapping(typeData, root, null, defaultNamespace); helper.RegisterClrType(map, type, map.XmlTypeNamespace); return(map); }
public XmlTypeMapping ImportTypeMapping(Type type, XmlRootAttribute root, string defaultNamespace) { if (type == null) { throw new ArgumentNullException("type"); } if (type == typeof(void)) { throw new InvalidOperationException("Type " + type.Name + " may not be serialized."); } if (defaultNamespace == null) { defaultNamespace = initialDefaultNamespace; } if (defaultNamespace == null) { defaultNamespace = string.Empty; } XmlTypeMapping map; switch (TypeTranslator.GetTypeData(type).SchemaType) { case SchemaTypes.Class: map = ImportClassMapping(type, root, defaultNamespace); break; case SchemaTypes.Array: map = ImportListMapping(type, root, defaultNamespace, null, 0); break; case SchemaTypes.XmlNode: map = ImportXmlNodeMapping(type, root, defaultNamespace); break; case SchemaTypes.Primitive: map = ImportPrimitiveMapping(type, root, defaultNamespace); break; case SchemaTypes.Enum: map = ImportEnumMapping(type, root, defaultNamespace); break; case SchemaTypes.XmlSerializable: map = ImportXmlSerializableMapping(type, root, defaultNamespace); break; default: throw new NotSupportedException("Type " + type.FullName + " not supported for XML stialization"); } map.RelatedMaps = relatedMaps; map.Format = SerializationFormat.Literal; map.Source = new XmlTypeSerializationSource(type, root, attributeOverrides, defaultNamespace, includedTypes); if (allowPrivateTypes) { map.Source.CanBeGenerated = false; } return(map); }
XmlTypeMapping ImportListMapping(Type type, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); XmlTypeMapping map = helper.GetRegisteredClrType(type, XmlSerializer.EncodingNamespace); if (map != null) { return(map); } ListMap obmap = new ListMap(); TypeData itemTypeData = typeData.ListItemTypeData; map = CreateTypeMapping(typeData, "Array", XmlSerializer.EncodingNamespace); helper.RegisterClrType(map, type, XmlSerializer.EncodingNamespace); map.MultiReferenceType = true; map.ObjectMap = obmap; XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(null, itemTypeData); if (elem.TypeData.IsComplexType) { elem.MappedType = ImportTypeMapping(typeData.ListItemType, defaultNamespace); elem.TypeData = elem.MappedType.TypeData; } elem.ElementName = "Item"; elem.Namespace = string.Empty; elem.IsNullable = true; // By default, items are nullable XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList(); list.Add(elem); obmap.ItemInfo = list; XmlTypeMapping objMap = ImportTypeMapping(typeof(object), defaultNamespace); objMap.DerivedTypes.Add(map); // Register any of the including types as a derived class of object SoapIncludeAttribute[] includes = (SoapIncludeAttribute[])type.GetCustomAttributes(typeof(SoapIncludeAttribute), false); for (int i = 0; i < includes.Length; i++) { Type includedType = includes[i].Type; objMap.DerivedTypes.Add(ImportTypeMapping(includedType, defaultNamespace)); } return(map); }
ICollection GetReflectionMembers(Type type) { ArrayList members = new ArrayList(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo prop in properties) { if (!prop.CanRead) { continue; } if (!prop.CanWrite && (TypeTranslator.GetTypeData(prop.PropertyType).SchemaType != SchemaTypes.Array || prop.PropertyType.IsArray)) { continue; } SoapAttributes atts = attributeOverrides[type, prop.Name]; if (atts == null) { atts = new SoapAttributes(prop); } if (atts.SoapIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts); members.Add(member); } FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo field in fields) { SoapAttributes atts = attributeOverrides[type, field.Name]; if (atts == null) { atts = new SoapAttributes(field); } if (atts.SoapIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts); members.Add(member); } return(members); }
XmlTypeMapping ImportXmlNodeMapping(Type type, XmlRootAttribute root, string defaultNamespace) { XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(TypeTranslator.GetTypeData(type), root, defaultNamespace)); if (map != null) { return(map); } // Registers the maps for XmlNode and XmlElement XmlTypeMapping nodeMap = CreateTypeMapping(TypeTranslator.GetTypeData(typeof(XmlNode)), root, null, defaultNamespace); helper.RegisterClrType(nodeMap, typeof(XmlNode), nodeMap.XmlTypeNamespace); XmlTypeMapping elemMap = CreateTypeMapping(TypeTranslator.GetTypeData(typeof(XmlElement)), root, null, defaultNamespace); helper.RegisterClrType(elemMap, typeof(XmlElement), elemMap.XmlTypeNamespace); XmlTypeMapping textMap = CreateTypeMapping(TypeTranslator.GetTypeData(typeof(XmlText)), root, null, defaultNamespace); helper.RegisterClrType(textMap, typeof(XmlText), textMap.XmlTypeNamespace); XmlTypeMapping docMap = CreateTypeMapping(TypeTranslator.GetTypeData(typeof(XmlDocument)), root, null, defaultNamespace); helper.RegisterClrType(docMap, typeof(XmlDocument), textMap.XmlTypeNamespace); XmlTypeMapping obmap = ImportTypeMapping(typeof(object)); obmap.DerivedTypes.Add(nodeMap); obmap.DerivedTypes.Add(elemMap); obmap.DerivedTypes.Add(textMap); obmap.DerivedTypes.Add(docMap); nodeMap.DerivedTypes.Add(elemMap); nodeMap.DerivedTypes.Add(textMap); nodeMap.DerivedTypes.Add(docMap); map = helper.GetRegisteredClrType(type, GetTypeNamespace(TypeTranslator.GetTypeData(type), root, defaultNamespace)); if (map == null) { throw new InvalidOperationException("Objects of type '" + type + "' can't be serialized"); } return(map); }
protected void WriteTypedPrimitive(string name, string ns, object o, bool xsiType) { string value; TypeData td = TypeTranslator.GetTypeData(o.GetType(), null, true); if (td.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(String.Format("The type of the argument object '{0}' is not primitive.", td.FullTypeName)); } if (name == null) { ns = td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace; name = td.XmlType; } else { name = XmlCustomFormatter.FromXmlName(name); } Writer.WriteStartElement(name, ns); if (o is XmlQualifiedName) { value = FromXmlQualifiedName((XmlQualifiedName)o); } else { value = XmlCustomFormatter.ToXmlString(td, o); } if (xsiType) { if (td.SchemaType != SchemaTypes.Primitive) { throw new InvalidOperationException(string.Format(unexpectedTypeError, o.GetType().FullName)); } WriteXsiType(td.XmlType, td.IsXsdType ? XmlSchema.Namespace : XmlSerializer.WsdlTypesNamespace); } WriteValue(value); Writer.WriteEndElement(); }
XmlTypeMapping ImportEnumMapping(Type type, XmlRootAttribute root, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, root, defaultNamespace)); if (map != null) { return(map); } map = CreateTypeMapping(typeData, root, null, defaultNamespace); helper.RegisterClrType(map, type, map.XmlTypeNamespace); string [] names = Enum.GetNames(type); ArrayList members = new ArrayList(); foreach (string name in names) { MemberInfo[] mem = type.GetMember(name); string xmlName = null; object[] atts = mem[0].GetCustomAttributes(typeof(XmlIgnoreAttribute), false); if (atts.Length > 0) { continue; } atts = mem[0].GetCustomAttributes(typeof(XmlEnumAttribute), false); if (atts.Length > 0) { xmlName = ((XmlEnumAttribute)atts[0]).Name; } if (xmlName == null) { xmlName = name; } members.Add(new EnumMap.EnumMapMember(xmlName, name)); } bool isFlags = type.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0; map.ObjectMap = new EnumMap((EnumMap.EnumMapMember[])members.ToArray(typeof(EnumMap.EnumMapMember)), isFlags); ImportTypeMapping(typeof(object)).DerivedTypes.Add(map); return(map); }
private ICollection GetReflectionMembers(Type type) { ArrayList arrayList = new ArrayList(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo propertyInfo in properties) { if (propertyInfo.CanRead) { if (propertyInfo.CanWrite || (TypeTranslator.GetTypeData(propertyInfo.PropertyType).SchemaType == SchemaTypes.Array && !propertyInfo.PropertyType.IsArray)) { SoapAttributes soapAttributes = this.attributeOverrides[type, propertyInfo.Name]; if (soapAttributes == null) { soapAttributes = new SoapAttributes(propertyInfo); } if (!soapAttributes.SoapIgnore) { XmlReflectionMember value = new XmlReflectionMember(propertyInfo.Name, propertyInfo.PropertyType, soapAttributes); arrayList.Add(value); } } } } FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo fieldInfo in fields) { SoapAttributes soapAttributes2 = this.attributeOverrides[type, fieldInfo.Name]; if (soapAttributes2 == null) { soapAttributes2 = new SoapAttributes(fieldInfo); } if (!soapAttributes2.SoapIgnore) { XmlReflectionMember value2 = new XmlReflectionMember(fieldInfo.Name, fieldInfo.FieldType, soapAttributes2); arrayList.Add(value2); } } return(arrayList); }
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); }
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); } }
public static TypeData GetTypeData(Type type) { return(TypeTranslator.GetTypeData(type, null)); }
public static bool IsPrimitive(Type type) { return(TypeTranslator.GetTypeData(type).SchemaType == SchemaTypes.Primitive); }
XmlTypeMapping ImportClassMapping(Type type, XmlRootAttribute root, string defaultNamespace) { TypeData typeData = TypeTranslator.GetTypeData(type); XmlTypeMapping map = helper.GetRegisteredClrType(type, GetTypeNamespace(typeData, root, defaultNamespace)); if (map != null) { return(map); } if (!allowPrivateTypes) { ReflectionHelper.CheckSerializableType(type); } map = CreateTypeMapping(typeData, root, null, defaultNamespace); helper.RegisterClrType(map, type, map.XmlTypeNamespace); helper.RegisterSchemaType(map, map.XmlType, map.XmlTypeNamespace); // Import members ClassMap classMap = new ClassMap(); map.ObjectMap = classMap; // try // { ICollection members = GetReflectionMembers(type); foreach (XmlReflectionMember rmember in members) { if (rmember.XmlAttributes.XmlIgnore) { continue; } XmlTypeMapMember mem = CreateMapMember(rmember, map.XmlTypeNamespace); mem.CheckOptionalValueType(type); classMap.AddMember(mem); } // } // catch (Exception ex) { // throw helper.CreateError (map, ex.Message); // } ImportIncludedTypes(type, defaultNamespace); // Import extra classes if (type == typeof(object) && includedTypes != null) { foreach (Type intype in includedTypes) { map.DerivedTypes.Add(ImportTypeMapping(intype, defaultNamespace)); } } // Register inheritance relations if (type.BaseType != null) { XmlTypeMapping bmap = ImportClassMapping(type.BaseType, root, defaultNamespace); if (type.BaseType != typeof(object)) { map.BaseMap = bmap; classMap.SetCanBeSimpleType(false); } // At this point, derived classes of this map must be already registered RegisterDerivedMap(bmap, map); if (((ClassMap)bmap.ObjectMap).HasSimpleContent && classMap.ElementMembers != null && classMap.ElementMembers.Count != 1) { throw new InvalidOperationException(String.Format(errSimple, map.TypeData.TypeName, map.BaseMap.TypeData.TypeName)); } } if (classMap.XmlTextCollector != null && !classMap.HasSimpleContent) { XmlTypeMapMember mem = classMap.XmlTextCollector; if (mem.TypeData.Type != typeof(string) && mem.TypeData.Type != typeof(string[]) && mem.TypeData.Type != typeof(object[]) && mem.TypeData.Type != typeof(XmlNode[])) { throw new InvalidOperationException(String.Format(errSimple2, map.TypeData.TypeName, mem.Name, mem.TypeData.TypeName)); } } return(map); }
XmlTypeMapElementInfoList ImportElementInfo(string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts) { XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList(); ImportTextElementInfo(list, defaultType, member, atts); if (atts.XmlElements.Count == 0 && list.Count == 0) { XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, TypeTranslator.GetTypeData(defaultType)); elem.ElementName = defaultName; elem.Namespace = defaultNamespace; if (elem.TypeData.IsComplexType) { elem.MappedType = ImportTypeMapping(defaultType, null, defaultNamespace); } list.Add(elem); } bool multiType = (atts.XmlElements.Count > 1); foreach (XmlElementAttribute att in atts.XmlElements) { Type elemType = (att.Type != null) ? att.Type : defaultType; XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, TypeTranslator.GetTypeData(elemType, att.DataType)); elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName; elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace; elem.Form = att.Form; elem.IsNullable = att.IsNullable; if (elem.IsNullable && elem.TypeData.IsValueType) { throw new InvalidOperationException("IsNullable may not be 'true' for value type " + elem.TypeData.FullTypeName + " in member '" + defaultName + "'"); } if (elem.TypeData.IsComplexType) { if (att.DataType != null) { throw new InvalidOperationException("'" + att.DataType + "' is an invalid value for the XmlElementAttribute.DateTime property. The property may only be specified for primitive types."); } elem.MappedType = ImportTypeMapping(elemType, null, elem.Namespace); } if (att.ElementName != null) { elem.ElementName = att.ElementName; } else if (multiType) { if (elem.MappedType != null) { elem.ElementName = elem.MappedType.ElementName; } else { elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType; } } else { elem.ElementName = defaultName; } list.Add(elem); } return(list); }
private XmlTypeMapMember CreateMapMember(XmlReflectionMember rmember, string defaultNamespace) { XmlTypeMapMember mapMember; XmlAttributes atts = rmember.XmlAttributes; TypeData typeData = TypeTranslator.GetTypeData(rmember.MemberType); if (atts.XmlAnyAttribute != null) { if ((rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]")) { mapMember = new XmlTypeMapMemberAnyAttribute(); } else { throw new InvalidOperationException("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]"); } } else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0) { if ((rmember.MemberType.FullName == "System.Xml.XmlElement[]") || (rmember.MemberType.FullName == "System.Xml.XmlNode[]") || (rmember.MemberType.FullName == "System.Xml.XmlElement")) { XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement(); member.ElementInfo = ImportAnyElementInfo(defaultNamespace, rmember, member, atts); mapMember = member; } else { throw new InvalidOperationException("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]"); } } else if (atts.Xmlns) { XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces(); mapMember = mapNamespaces; } else if (atts.XmlAttribute != null) { // An attribute if (atts.XmlElements != null && atts.XmlElements.Count > 0) { throw new Exception("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member"); } XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute(); if (atts.XmlAttribute.AttributeName == null) { mapAttribute.AttributeName = rmember.MemberName; } else { mapAttribute.AttributeName = atts.XmlAttribute.AttributeName; } if (typeData.IsComplexType) { mapAttribute.MappedType = ImportTypeMapping(typeData.Type, null, mapAttribute.Namespace); } if (atts.XmlAttribute.Namespace != null && atts.XmlAttribute.Namespace != defaultNamespace) { if (atts.XmlAttribute.Form == XmlSchemaForm.Unqualified) { throw new InvalidOperationException("The Form property may not be 'Unqualified' when an explicit Namespace property is present"); } mapAttribute.Form = XmlSchemaForm.Qualified; mapAttribute.Namespace = atts.XmlAttribute.Namespace; } else { mapAttribute.Form = atts.XmlAttribute.Form; if (atts.XmlAttribute.Form == XmlSchemaForm.Qualified) { mapAttribute.Namespace = defaultNamespace; } else { mapAttribute.Namespace = ""; } } typeData = TypeTranslator.GetTypeData(rmember.MemberType, atts.XmlAttribute.DataType); mapMember = mapAttribute; } else if (typeData.SchemaType == SchemaTypes.Array) { // If the member has a single XmlElementAttribute and the type is the type of the member, // then it is not a flat list if (atts.XmlElements.Count > 1 || (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) || (atts.XmlText != null)) { // A flat list // TODO: check that it does not have XmlArrayAttribute XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList(); member.ListMap = new ListMap(); member.ListMap.ItemInfo = ImportElementInfo(rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts); member.ElementInfo = member.ListMap.ItemInfo; mapMember = member; } else { // A list XmlTypeMapMemberList member = new XmlTypeMapMemberList(); // Creates an ElementInfo that identifies the array instance. member.ElementInfo = new XmlTypeMapElementInfoList(); XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo(member, typeData); elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName; elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace; elem.MappedType = ImportListMapping(rmember.MemberType, null, elem.Namespace, atts, 0); elem.IsNullable = (atts.XmlArray != null) ? atts.XmlArray.IsNullable : false; elem.Form = (atts.XmlArray != null) ? atts.XmlArray.Form : XmlSchemaForm.Qualified; member.ElementInfo.Add(elem); mapMember = member; } } else { // An element XmlTypeMapMemberElement member = new XmlTypeMapMemberElement(); member.ElementInfo = ImportElementInfo(rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts); mapMember = member; } mapMember.DefaultValue = atts.XmlDefaultValue; mapMember.TypeData = typeData; mapMember.Name = rmember.MemberName; mapMember.IsReturnValue = rmember.IsReturnValue; return(mapMember); }
ICollection GetReflectionMembers(Type type) { // First we want to find the inheritance hierarchy in reverse order. Type currentType = type; ArrayList typeList = new ArrayList(); typeList.Add(currentType); while (currentType != typeof(object)) { currentType = currentType.BaseType; // Read the base type. typeList.Insert(0, currentType); // Insert at 0 to reverse the order. } // Read all Fields via reflection. ArrayList fieldList = new ArrayList(); FieldInfo[] tfields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); currentType = null; int currentIndex = 0; foreach (FieldInfo field in tfields) { // This statement ensures fields are ordered starting from the base type. if (currentType != field.DeclaringType) { currentType = field.DeclaringType; currentIndex = 0; } fieldList.Insert(currentIndex++, field); } // Read all Properties via reflection. ArrayList propList = new ArrayList(); PropertyInfo[] tprops = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); currentType = null; currentIndex = 0; foreach (PropertyInfo prop in tprops) { // This statement ensures properties are ordered starting from the base type. if (currentType != prop.DeclaringType) { currentType = prop.DeclaringType; currentIndex = 0; } if (!prop.CanRead) { continue; } if (!prop.CanWrite && TypeTranslator.GetTypeData(prop.PropertyType).SchemaType != SchemaTypes.Array) { continue; } if (prop.GetIndexParameters().Length > 0) { continue; } propList.Insert(currentIndex++, prop); } ArrayList members = new ArrayList(); int fieldIndex = 0; int propIndex = 0; // We now step through the type hierarchy from the base (object) through // to the supplied class, as each step outputting all Fields, and then // all Properties. This is the exact same ordering as .NET 1.0/1.1. foreach (Type t in typeList) { // Add any fields matching the current DeclaringType. while (fieldIndex < fieldList.Count) { FieldInfo field = (FieldInfo)fieldList[fieldIndex]; if (field.DeclaringType == t) { fieldIndex++; XmlAttributes atts = attributeOverrides[type, field.Name]; if (atts == null) { atts = new XmlAttributes(field); } if (atts.XmlIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts); members.Add(member); } else { break; } } // Add any properties matching the current DeclaringType. while (propIndex < propList.Count) { PropertyInfo prop = (PropertyInfo)propList[propIndex]; if (prop.DeclaringType == t) { propIndex++; XmlAttributes atts = attributeOverrides[type, prop.Name]; if (atts == null) { atts = new XmlAttributes(prop); } if (atts.XmlIgnore) { continue; } XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts); members.Add(member); } else { break; } } } return(members); }