using System.Globalization;
using System.Text;
+using HookDir = System.Xml.Serialization.XmlMappingAccess;
+
namespace System.Xml.Serialization
{
internal class SerializationCodeGenerator
_writer = writer;
_results = new GenerationResult [_xmlMaps.Length];
+ WriteLine ("// It is automatically generated");
WriteLine ("using System;");
WriteLine ("using System.Xml;");
WriteLine ("using System.Xml.Schema;");
WriteLine ("");
}
+ WriteLine ("#if !TARGET_JVM"); // does it make sense? ;-)
WriteLine (access_mod + " class " + main.ImplementationClassName + " : System.Xml.Serialization.XmlSerializerImplementation");
WriteLineInd ("{");
WriteLineUni ("}");
WriteLine ("");
+ WriteLine ("#endif");
}
#endif
WriteLine ("internal class " + writerClassName + " : XmlSerializationWriter");
WriteLineInd ("{");
WriteLine ("const string xmlNamespace = \"http://www.w3.org/2000/xmlns/\";");
+ // ToBinHexString() is not public, so use reflection here.
+ WriteLine ("static readonly System.Reflection.MethodInfo toBinHexStringMethod = typeof (XmlConvert).GetMethod (\"ToBinHexString\", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, null, new Type [] {typeof (byte [])}, null);");
+ WriteLine ("static string ToBinHexString (byte [] input)");
+ WriteLineInd ("{");
+ WriteLine ("return input == null ? null : (string) toBinHexStringMethod.Invoke (null, new object [] {input});");
+ WriteLineUni ("}");
for (int n=0; n<maps.Count; n++)
{
WriteLineInd ("switch (val) {");
for (int i = 0; i < emap.EnumNames.Length; i++)
- WriteLine ("case " + map.TypeData.CSharpFullName + "." + emap.EnumNames[i] + ": return " + GetLiteral (emap.XmlNames[i]) + ";");
+ WriteLine ("case " + map.TypeData.CSharpFullName + ".@" + emap.EnumNames[i] + ": return " + GetLiteral (emap.XmlNames[i]) + ";");
if (emap.IsFlags) {
WriteLineInd ("default:");
SetHookVar ("$TYPE", typeMap.TypeData.CSharpName);
SetHookVar ("$FULLTYPE", typeMap.TypeData.CSharpFullName);
SetHookVar ("$OBJECT", "ob");
- SetHookVar ("$ELEMENT", "element");
- SetHookVar ("$NAMESPACE", "namesp");
SetHookVar ("$NULLABLE", "isNullable");
if (GenerateWriteHook (HookType.type, typeMap.TypeData.Type))
if (typeMap.TypeData.Type == typeof (object)) {
WriteLineInd ("else {");
- WriteLine ("WriteTypedPrimitive (element, namesp, ob, true);");
+ WriteLineInd ("if (ob.GetType().IsArray && typeof(XmlNode).IsAssignableFrom(ob.GetType().GetElementType())) {");
+ WriteLine ("Writer.WriteStartElement (" + GetLiteral (typeMap.ElementName) + ", " + GetLiteral (typeMap.Namespace) + ");");
+ WriteLineInd ("foreach (XmlNode node in (System.Collections.IEnumerable) ob)");
+ WriteLineUni ("node.WriteTo (Writer);");
+ WriteLineUni ("Writer.WriteEndElement ();");
+ WriteLine ("}");
+ WriteLineInd ("else");
+ WriteLineUni ("WriteTypedPrimitive (element, namesp, ob, true);");
WriteLine ("return;");
WriteLineUni ("}");
}
if (cond != null) WriteLineInd ("if (" + cond + ") {");
- string strVal = GenerateGetStringValue (attr.MappedType, attr.TypeData, val);
+ string strVal = GenerateGetStringValue (attr.MappedType, attr.TypeData, val, false);
WriteLine ("WriteAttribute (" + GetLiteral(attr.AttributeName) + ", " + GetLiteral(attr.Namespace) + ", " + strVal + ");");
if (cond != null) WriteLineUni ("}");
break;
case SchemaTypes.XmlSerializable:
- WriteMetCall ("WriteSerializable", memberValue, GetLiteral(elem.ElementName), GetLiteral(elem.Namespace), GetLiteral(elem.IsNullable));
+ WriteMetCall ("WriteSerializable", "(" + ToCSharpFullName (elem.MappedType.TypeData.Type) + ") " + memberValue, GetLiteral(elem.ElementName), GetLiteral(elem.Namespace), GetLiteral(elem.IsNullable));
break;
default:
void GenerateWriteAnyElementContent (XmlTypeMapMemberAnyElement member, string memberValue)
{
bool singleElement = (member.TypeData.Type == typeof (XmlElement));
- string var;
+ string var, var2;
+ var2 = GetObTempVar ();
if (singleElement)
var = memberValue;
else {
var = GetObTempVar ();
- WriteLineInd ("foreach (XmlNode " + var + " in " + memberValue + ") {");
+ WriteLineInd ("foreach (object " + var2 + " in " + memberValue + ") {");
}
-
+ WriteLine ("XmlNode " + var + " = " + var2 + " as XmlNode;");
+ WriteLine ("if (" + var + " == null && " + var2 + "!= null) throw new InvalidOperationException (\"A member with XmlAnyElementAttribute can only serialize and deserialize certain XmlNode types.");
+ WriteLineUni ("}");
+
string elem = GetObTempVar ();
WriteLine ("XmlNode " + elem + " = " + var + ";");
WriteLine ("if (" + elem + " is XmlElement) {");
if (!member.IsDefaultAny) {
for (int n=0; n<member.ElementInfo.Count; n++) {
XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)member.ElementInfo[n];
- string txt = "(" + elem + ".Name == " + GetLiteral(info.ElementName) + " && " + elem + ".NamespaceURI == " + GetLiteral(info.Namespace) + ")";
+ string txt = "(" + elem + ".LocalName == " + GetLiteral(info.ElementName) + " && " + elem + ".NamespaceURI == " + GetLiteral(info.Namespace) + ")";
if (n == member.ElementInfo.Count-1) txt += ") {";
if (n == 0) WriteLineInd ("if (" + txt);
else WriteLine ("|| " + txt);
void GenerateWritePrimitiveElement (XmlTypeMapping typeMap, string ob)
{
- string strVal = GenerateGetStringValue (typeMap, typeMap.TypeData, ob);
+ string strVal = GenerateGetStringValue (typeMap, typeMap.TypeData, ob, false);
WriteLine ("Writer.WriteString (" + strVal + ");");
}
WriteLine ("Writer.WriteString (" + strVal + ");");
}
- string GenerateGetStringValue (XmlTypeMapping typeMap, TypeData type, string value)
+ string GenerateGetStringValue (XmlTypeMapping typeMap, TypeData type, string value, bool isNullable)
{
if (type.SchemaType == SchemaTypes.Array) {
string str = GetStrTempVar ();
return str;
}
else if (type.SchemaType == SchemaTypes.Enum) {
- return GenerateGetEnumXmlValue (typeMap, value);
+ if (isNullable)
+ return "(" + value + ").HasValue ? " + GenerateGetEnumXmlValue (typeMap, "(" + value + ").Value") + " : null";
+ else
+ return GenerateGetEnumXmlValue (typeMap, value);
}
else if (type.Type == typeof (XmlQualifiedName))
return "FromXmlQualifiedName (" + value + ")";
GenerateWriteMemberElement (info, GetCast (info.TypeData, itemTypeData, item));
else
{
- string strVal = GenerateGetStringValue (info.MappedType, info.TypeData, GetCast (info.TypeData, itemTypeData, item));
+ string strVal = GenerateGetStringValue (info.MappedType, info.TypeData, GetCast (info.TypeData, itemTypeData, item), false);
WriteLine (targetString + ".Append (" + strVal + ").Append (\" \");");
}
void GenerateWritePrimitiveValueLiteral (string memberValue, string name, string ns, XmlTypeMapping mappedType, TypeData typeData, bool wrapped, bool isNullable)
{
if (!wrapped) {
- string strVal = GenerateGetStringValue (mappedType, typeData, memberValue);
+ string strVal = GenerateGetStringValue (mappedType, typeData, memberValue, false);
WriteMetCall ("WriteValue", strVal);
}
else if (isNullable) {
if (typeData.Type == typeof(XmlQualifiedName))
WriteMetCall ("WriteNullableQualifiedNameLiteral", GetLiteral(name), GetLiteral(ns), memberValue);
else {
- string strVal = GenerateGetStringValue (mappedType, typeData, memberValue);
+ string strVal = GenerateGetStringValue (mappedType, typeData, memberValue, true);
WriteMetCall ("WriteNullableStringLiteral", GetLiteral(name), GetLiteral(ns), strVal);
}
}
if (typeData.Type == typeof(XmlQualifiedName))
WriteMetCall ("WriteElementQualifiedName", GetLiteral(name), GetLiteral(ns), memberValue);
else {
- string strVal = GenerateGetStringValue (mappedType, typeData, memberValue);
+ string strVal = GenerateGetStringValue (mappedType, typeData, memberValue, false);
WriteMetCall ("WriteElementString", GetLiteral(name),GetLiteral(ns), strVal);
}
}
void GenerateWritePrimitiveValueEncoded (string memberValue, string name, string ns, XmlQualifiedName xsiType, XmlTypeMapping mappedType, TypeData typeData, bool wrapped, bool isNullable)
{
if (!wrapped) {
- string strVal = GenerateGetStringValue (mappedType, typeData, memberValue);
+ string strVal = GenerateGetStringValue (mappedType, typeData, memberValue, false);
WriteMetCall ("WriteValue", strVal);
}
else if (isNullable) {
if (typeData.Type == typeof(XmlQualifiedName))
WriteMetCall ("WriteNullableQualifiedNameEncoded", GetLiteral(name), GetLiteral(ns), memberValue, GetLiteral(xsiType));
else {
- string strVal = GenerateGetStringValue (mappedType, typeData, memberValue);
+ string strVal = GenerateGetStringValue (mappedType, typeData, memberValue, true);
WriteMetCall ("WriteNullableStringEncoded", GetLiteral(name), GetLiteral(ns), strVal, GetLiteral(xsiType));
}
}
if (typeData.Type == typeof(XmlQualifiedName))
WriteMetCall ("WriteElementQualifiedName", GetLiteral(name), GetLiteral(ns), memberValue, GetLiteral(xsiType));
else {
- string strVal = GenerateGetStringValue (mappedType, typeData, memberValue);
+ string strVal = GenerateGetStringValue (mappedType, typeData, memberValue, false);
WriteMetCall ("WriteElementString", GetLiteral(name),GetLiteral(ns), strVal, GetLiteral(xsiType));
}
}
else
WriteLine ("internal class " + readerClassName + " : XmlSerializationReader");
WriteLineInd ("{");
+ // FromBinHexString() is not public, so use reflection here.
+ WriteLine ("static readonly System.Reflection.MethodInfo fromBinHexStringMethod = typeof (XmlConvert).GetMethod (\"FromBinHexString\", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic, null, new Type [] {typeof (string)}, null);");
+ WriteLine ("static byte [] FromBinHexString (string input)");
+ WriteLineInd ("{");
+ WriteLine ("return input == null ? null : (byte []) fromBinHexStringMethod.Invoke (null, new object [] {input});");
+ WriteLineUni ("}");
_mapsToGenerate = new ArrayList ();
_fixupCallbacks = new ArrayList ();
GenerateReadObject (map);
if (map.TypeData.SchemaType == SchemaTypes.Enum)
- GenerateGetEnumValue (map);
+ GenerateGetEnumValueMethod (map);
}
GenerateReadInitCallbacks ();
WriteLineInd ("{");
WriteLineInd ("if (Reader.IsEmptyElement) {");
WriteLine ("Reader.Skip();");
+ WriteLine ("Reader.MoveToContent();");
WriteLineUni ("}");
WriteLineInd ("else {");
WriteLine ("Reader.ReadStartElement();");
}
else
{
- WriteLine ("while (Reader.NodeType != System.Xml.XmlNodeType.EndElement)");
+ // bug #79988: out parameters need to be initialized if
+ // they are value types
+ ClassMap classMap = (ClassMap) typeMap.ObjectMap;
+ ArrayList members = classMap.AllMembers;
+ for (int n = 0; n < members.Count; n++) {
+ XmlTypeMapMember mem = (XmlTypeMapMember) members [n];
+ if (!mem.IsReturnValue && mem.TypeData.IsValueType)
+ GenerateSetMemberValueFromAttr (mem, "parameters",
+ String.Format ("({0}) Activator.CreateInstance(typeof({0}), true)", mem.TypeData.FullTypeName), true);
+ }
+
+ WriteLine ("while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && Reader.ReadState == ReadState.Interactive)");
WriteLineInd ("{");
WriteLine ("if (Reader.IsStartElement(" + GetLiteral(typeMap.ElementName) + ", " + GetLiteral(typeMap.Namespace) + "))");
WriteLineInd ("{");
- WriteLine ("if (Reader.IsEmptyElement) { Reader.Skip(); Reader.MoveToContent(); continue; }");
+ bool dummy = false;
+ GenerateReadAttributeMembers (typeMap, (ClassMap)typeMap.ObjectMap, "parameters", true, ref dummy);
+ WriteLine ("if (Reader.IsEmptyElement)");
+ WriteLineInd ("{");
+ WriteLine ("Reader.Skip(); Reader.MoveToContent(); continue;");
+ WriteLineUni ("}");
WriteLine ("Reader.ReadStartElement();");
GenerateReadMembers (typeMap, (ClassMap)typeMap.ObjectMap, "parameters", true, false);
WriteLine ("ReadEndElement();");
}
else
{
- WriteLine (typeMap.TypeData.CSharpFullName + " ob = new " + typeMap.TypeData.CSharpFullName + " ();");
+ WriteLine (typeMap.TypeData.CSharpFullName + String.Format (" ob = ({0}) Activator.CreateInstance(typeof({0}), true);", typeMap.TypeData.CSharpFullName));
if (GenerateReadHook (HookType.type, typeMap.TypeData.Type)) {
WriteLine ("return ob;");
}
WriteLine ("");
- WriteLine ("ob = new " + typeMap.TypeData.CSharpFullName + " ();");
+ WriteLine (String.Format ("ob = ({0}) Activator.CreateInstance(typeof({0}), true);", typeMap.TypeData.CSharpFullName));
}
WriteLine ("");
XmlTypeMapping typeMap = xmlMap as XmlTypeMapping;
Type xmlMapType = (typeMap != null) ? typeMap.TypeData.Type : typeof(object[]);
- // Load the default value of members
- if (map.MembersWithDefault != null)
- {
- ArrayList members = map.MembersWithDefault;
- for (int n=0; n<members.Count; n++) {
- XmlTypeMapMember mem = (XmlTypeMapMember) members[n];
- GenerateSetMemberValueFromAttr (mem, ob, GetLiteral (mem.DefaultValue), isValueList);
- }
- }
-
- // A value list cannot have attributes
-
- bool first;
- if (!isValueList && !GenerateReadHook (HookType.attributes, xmlMapType))
- {
- // Reads attributes
-
- XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
-
- if (anyAttrMember != null)
- {
- WriteLine ("int anyAttributeIndex = 0;");
- WriteLine (anyAttrMember.TypeData.CSharpFullName + " anyAttributeArray = null;");
- }
-
- WriteLine ("while (Reader.MoveToNextAttribute())");
- WriteLineInd ("{");
- first = true;
- if (map.AttributeMembers != null) {
- foreach (XmlTypeMapMemberAttribute at in map.AttributeMembers)
- {
- WriteLineInd ((first?"":"else ") + "if (Reader.LocalName == " + GetLiteral (at.AttributeName) + " && Reader.NamespaceURI == " + GetLiteral (at.Namespace) + ") {");
- if (!GenerateReadMemberHook (xmlMapType, at)) {
- GenerateSetMemberValue (at, ob, GenerateGetValueFromXmlString ("Reader.Value", at.TypeData, at.MappedType), isValueList);
- GenerateEndHook ();
- }
- WriteLineUni ("}");
- first = false;
- }
- }
- WriteLineInd ((first?"":"else ") + "if (IsXmlnsAttribute (Reader.Name)) {");
-
- // If the map has NamespaceDeclarations,
- // then store this xmlns to the given member.
- // If the instance doesn't exist, then create.
-
- if (map.NamespaceDeclarations != null) {
- if (!GenerateReadMemberHook (xmlMapType, map.NamespaceDeclarations)) {
- string nss = ob + ".@" + map.NamespaceDeclarations.Name;
- WriteLine ("if (" + nss + " == null) " + nss + " = new XmlSerializerNamespaces ();");
- WriteLineInd ("if (Reader.Prefix == \"xmlns\")");
- WriteLine (nss + ".Add (Reader.LocalName, Reader.Value);");
- Unindent ();
- WriteLineInd ("else");
- WriteLine (nss + ".Add (\"\", Reader.Value);");
- Unindent ();
- GenerateEndHook ();
- }
- }
-
- WriteLineUni ("}");
- WriteLineInd ("else {");
-
- if (anyAttrMember != null)
- {
- if (!GenerateReadArrayMemberHook (xmlMapType, anyAttrMember, "anyAttributeIndex")) {
- WriteLine ("System.Xml.XmlAttribute attr = (System.Xml.XmlAttribute) Document.ReadNode(Reader);");
- if (typeof(System.Xml.Schema.XmlSchemaAnnotated).IsAssignableFrom (xmlMapType))
- WriteLine ("ParseWsdlArrayType (attr);");
- GenerateAddListValue (anyAttrMember.TypeData, "anyAttributeArray", "anyAttributeIndex", GetCast (anyAttrMember.TypeData.ListItemTypeData, "attr"), true);
- GenerateEndHook ();
- }
- WriteLine ("anyAttributeIndex++;");
- }
- else {
- if (!GenerateReadHook (HookType.unknownAttribute, xmlMapType)) {
- WriteLine ("UnknownNode (" + ob + ");");
- GenerateEndHook ();
- }
- }
-
- WriteLineUni ("}");
- WriteLineUni ("}");
-
- if (anyAttrMember != null && !MemberHasReadReplaceHook (xmlMapType, anyAttrMember))
- {
- WriteLine ("");
- WriteLine("anyAttributeArray = (" + anyAttrMember.TypeData.CSharpFullName + ") ShrinkArray (anyAttributeArray, anyAttributeIndex, " + GetTypeOf(anyAttrMember.TypeData.Type.GetElementType()) + ", true);");
- GenerateSetMemberValue (anyAttrMember, ob, "anyAttributeArray", isValueList);
- }
- WriteLine ("");
-
- GenerateEndHook ();
- }
+ bool first = false;
+ // Read attributes
+ GenerateReadAttributeMembers (xmlMap, map, ob, isValueList, ref first);
if (!isValueList)
{
string[] readFlag = null;
if (map.ElementMembers != null && !readByOrder)
{
- string readFlagsVars = "bool ";
+ string readFlagsVars = string.Empty;
readFlag = new string[map.ElementMembers.Count];
- for (int n=0; n<map.ElementMembers.Count; n++) {
- readFlag[n] = GetBoolTempVar ();
- if (n > 0) readFlagsVars += ", ";
- readFlagsVars += readFlag[n] + "=false";
+ int n=0;
+ foreach (XmlTypeMapMember mem in map.ElementMembers) {
+ // The text collector doesn't need a flag
+ if (!((mem is XmlTypeMapMemberElement) && ((XmlTypeMapMemberElement)mem).IsXmlTextCollector)) {
+ readFlag[n] = GetBoolTempVar ();
+ if (readFlagsVars.Length > 0) readFlagsVars += ", ";
+ readFlagsVars += readFlag[n] + "=false";
+ }
+ n++;
+ }
+ if (readFlagsVars.Length > 0) {
+ readFlagsVars = "bool " + readFlagsVars;
+ WriteLine (readFlagsVars + ";");
}
- if (map.ElementMembers.Count > 0) WriteLine (readFlagsVars + ";");
WriteLine ("");
}
XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) mem.ElementInfo [0];
if (info.TypeData.Type == typeof (string))
GenerateSetMemberValue (mem, ob, "ReadString (" + GenerateGetMemberValue (mem, ob, isValueList) + ")", isValueList);
- else
- GenerateSetMemberValue (mem, ob, GenerateGetValueFromXmlString ("Reader.ReadString()", info.TypeData, info.MappedType), isValueList);
+ else {
+ WriteLineInd ("{");
+ string str = GetStrTempVar ();
+ WriteLine ("string " + str + " = Reader.ReadString();");
+ GenerateSetMemberValue (mem, ob, GenerateGetValueFromXmlString (str, info.TypeData, info.MappedType, info.IsNullable), isValueList);
+ WriteLineUni ("}");
+ }
GenerateEndHook ();
}
WriteLineUni ("}");
WriteLine ("ReadEndElement();");
}
}
+
+ void GenerateReadAttributeMembers (XmlMapping xmlMap, ClassMap map, string ob, bool isValueList, ref bool first)
+ {
+ XmlTypeMapping typeMap = xmlMap as XmlTypeMapping;
+ Type xmlMapType = (typeMap != null) ? typeMap.TypeData.Type : typeof(object[]);
+
+ if (GenerateReadHook (HookType.attributes, xmlMapType))
+ return;
+
+ XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
+
+ if (anyAttrMember != null)
+ {
+ WriteLine ("int anyAttributeIndex = 0;");
+ WriteLine (anyAttrMember.TypeData.CSharpFullName + " anyAttributeArray = null;");
+ }
+
+ WriteLine ("while (Reader.MoveToNextAttribute())");
+ WriteLineInd ("{");
+ first = true;
+ if (map.AttributeMembers != null) {
+ foreach (XmlTypeMapMemberAttribute at in map.AttributeMembers)
+ {
+ WriteLineInd ((first?"":"else ") + "if (Reader.LocalName == " + GetLiteral (at.AttributeName) + " && Reader.NamespaceURI == " + GetLiteral (at.Namespace) + ") {");
+ if (!GenerateReadMemberHook (xmlMapType, at)) {
+ GenerateSetMemberValue (at, ob, GenerateGetValueFromXmlString ("Reader.Value", at.TypeData, at.MappedType, false), isValueList);
+ GenerateEndHook ();
+ }
+ WriteLineUni ("}");
+ first = false;
+ }
+ }
+ WriteLineInd ((first?"":"else ") + "if (IsXmlnsAttribute (Reader.Name)) {");
+
+ // If the map has NamespaceDeclarations,
+ // then store this xmlns to the given member.
+ // If the instance doesn't exist, then create.
+
+ if (map.NamespaceDeclarations != null) {
+ if (!GenerateReadMemberHook (xmlMapType, map.NamespaceDeclarations)) {
+ string nss = ob + ".@" + map.NamespaceDeclarations.Name;
+ WriteLine ("if (" + nss + " == null) " + nss + " = new XmlSerializerNamespaces ();");
+ WriteLineInd ("if (Reader.Prefix == \"xmlns\")");
+ WriteLine (nss + ".Add (Reader.LocalName, Reader.Value);");
+ Unindent ();
+ WriteLineInd ("else");
+ WriteLine (nss + ".Add (\"\", Reader.Value);");
+ Unindent ();
+ GenerateEndHook ();
+ }
+ }
+
+ WriteLineUni ("}");
+ WriteLineInd ("else {");
+
+ if (anyAttrMember != null)
+ {
+ if (!GenerateReadArrayMemberHook (xmlMapType, anyAttrMember, "anyAttributeIndex")) {
+ WriteLine ("System.Xml.XmlAttribute attr = (System.Xml.XmlAttribute) Document.ReadNode(Reader);");
+ if (typeof(System.Xml.Schema.XmlSchemaAnnotated).IsAssignableFrom (xmlMapType))
+ WriteLine ("ParseWsdlArrayType (attr);");
+ GenerateAddListValue (anyAttrMember.TypeData, "anyAttributeArray", "anyAttributeIndex", GetCast (anyAttrMember.TypeData.ListItemTypeData, "attr"), true);
+ GenerateEndHook ();
+ }
+ WriteLine ("anyAttributeIndex++;");
+ }
+ else {
+ if (!GenerateReadHook (HookType.unknownAttribute, xmlMapType)) {
+ WriteLine ("UnknownNode (" + ob + ");");
+ GenerateEndHook ();
+ }
+ }
+
+ WriteLineUni ("}");
+ WriteLineUni ("}");
+
+ if (anyAttrMember != null && !MemberHasReadReplaceHook (xmlMapType, anyAttrMember))
+ {
+ WriteLine ("");
+ WriteLine("anyAttributeArray = (" + anyAttrMember.TypeData.CSharpFullName + ") ShrinkArray (anyAttributeArray, anyAttributeIndex, " + GetTypeOf(anyAttrMember.TypeData.Type.GetElementType()) + ", true);");
+ GenerateSetMemberValue (anyAttrMember, ob, "anyAttributeArray", isValueList);
+ }
+ WriteLine ("");
+ WriteLine ("Reader.MoveToElement ();");
+
+ GenerateEndHook ();
+ }
void GenerateSetListMembersDefaults (XmlTypeMapping typeMap, ClassMap map, string ob, bool isValueList)
{
return GetReadObjectCall (elem.MappedType, GetLiteral(elem.IsNullable), "true");
case SchemaTypes.XmlSerializable:
- return GetCast (elem.TypeData, "ReadSerializable (new " + elem.TypeData.CSharpFullName + " ())");
+ return GetCast (elem.TypeData, String.Format ("({0}) ReadSerializable (({0}) Activator.CreateInstance(typeof({0}), true))", elem.TypeData.CSharpFullName));
default:
throw new NotSupportedException ("Invalid value type");
if (elem.IsNullable) return "ReadNullableQualifiedName ()";
else return "ReadElementQualifiedName ()";
}
- else if (elem.IsNullable)
- return GenerateGetValueFromXmlString ("ReadNullableString ()", elem.TypeData, elem.MappedType);
- else
- return GenerateGetValueFromXmlString ("Reader.ReadElementString ()", elem.TypeData, elem.MappedType);
+ else if (elem.IsNullable) {
+ string str = GetStrTempVar ();
+ WriteLine ("string " + str + " = ReadNullableString ();");
+ return GenerateGetValueFromXmlString (str, elem.TypeData, elem.MappedType, true);
+ }
+ else {
+ string str = GetStrTempVar ();
+ WriteLine ("string " + str + " = Reader.ReadElementString ();");
+ return GenerateGetValueFromXmlString (str, elem.TypeData, elem.MappedType, false);
+ }
}
- string GenerateGetValueFromXmlString (string value, TypeData typeData, XmlTypeMapping typeMap)
+ string GenerateGetValueFromXmlString (string value, TypeData typeData, XmlTypeMapping typeMap, bool isNullable)
{
if (typeData.SchemaType == SchemaTypes.Array)
return GenerateReadListString (typeMap, value);
else if (typeData.SchemaType == SchemaTypes.Enum)
- return GenerateGetEnumValue (typeMap, value);
+ return GenerateGetEnumValue (typeMap, value, isNullable);
else if (typeData.Type == typeof (XmlQualifiedName))
return "ToXmlQualifiedName (" + value + ")";
else
string index = GetNumTempVar ();
WriteLineInd ("for (int " + index + " = 0; " + index + " < " + valueArray + ".Length; " + index + "++)");
- WriteLine (list + "[" + index + "] = " + GenerateGetValueFromXmlString (valueArray + "[" + index + "]", info.TypeData, info.MappedType) + ";");
+ WriteLine (list + "[" + index + "] = " + GenerateGetValueFromXmlString (valueArray + "[" + index + "]", info.TypeData, info.MappedType, info.IsNullable) + ";");
Unindent ();
WriteLineUni ("}");
WriteLine ("else");
{
WriteLine ("if (((object)" + list + ") == null)");
if (canCreateInstance)
- WriteLine ("\t" + list + " = new " + listType.CSharpFullName + "();");
+ WriteLine ("\t" + list + String.Format (" = ({0}) Activator.CreateInstance(typeof({0}), true);", listType.CSharpFullName));
else
WriteLine ("\tthrow CreateReadOnlyCollectionException (" + GetLiteral (listType.CSharpFullName) + ");");
void GenerateReadEnumElement (XmlTypeMapping typeMap, string isNullable)
{
WriteLine ("Reader.ReadStartElement ();");
- WriteLine (typeMap.TypeData.CSharpFullName + " res = " + GenerateGetEnumValue (typeMap, "Reader.ReadString()") + ";");
+ WriteLine (typeMap.TypeData.CSharpFullName + " res = " + GenerateGetEnumValue (typeMap, "Reader.ReadString()", false) + ";");
WriteLineInd ("if (Reader.NodeType != XmlNodeType.None)");
WriteLineUni ("Reader.ReadEndElement ();");
WriteLine ("return res;");
}
- string GenerateGetEnumValue (XmlTypeMapping typeMap, string val)
+ string GenerateGetEnumValue (XmlTypeMapping typeMap, string val, bool isNullable)
{
- return GetGetEnumValueName (typeMap) + " (" + val + ")";
+ if (isNullable)
+ return "(" + val + ") != null ? " + GetGetEnumValueName (typeMap) + " (" + val + ") : (" + typeMap.TypeData.CSharpFullName + "?) null";
+ else
+ return GetGetEnumValueName (typeMap) + " (" + val + ")";
}
- void GenerateGetEnumValue (XmlTypeMapping typeMap)
+ void GenerateGetEnumValueMethod (XmlTypeMapping typeMap)
{
string metName = GetGetEnumValueName (typeMap);
EnumMap map = (EnumMap) typeMap.ObjectMap;
WriteLineInd ("{");
foreach (EnumMap.EnumMapMember mem in map.Members)
{
- WriteLine ("case " + GetLiteral (mem.XmlName) + ": return " + typeMap.TypeData.CSharpFullName + "." + mem.EnumName + ";");
+ WriteLine ("case " + GetLiteral (mem.XmlName) + ": return " + typeMap.TypeData.CSharpFullName + ".@" + mem.EnumName + ";");
}
WriteLineInd ("default:");
WriteLine ("throw CreateUnknownConstantException (" + val + ", typeof(" + typeMap.TypeData.CSharpFullName + "));");
WriteLine ("if (Reader.NodeType == XmlNodeType.Element)");
WriteLineInd ("{");
WriteLine ("if (Reader.LocalName == " + GetLiteral (typeMap.ElementName) + " && Reader.NamespaceURI == " + GetLiteral (typeMap.Namespace) + ")");
- WriteLine ("\treturn ReadSerializable (new " + typeMap.TypeData.CSharpFullName + "());");
+ WriteLine (String.Format ("\treturn ({0}) ReadSerializable (({0}) Activator.CreateInstance(typeof({0}), true));", typeMap.TypeData.CSharpFullName));
WriteLine ("else");
WriteLine ("\tthrow CreateUnknownNodeException ();");
WriteLineUni ("}");
WriteLine ("else UnknownNode (null);");
WriteLine ("");
- WriteLine ("return null;");
+ WriteLine ("return default (" + typeMap.TypeData.CSharpFullName + ");");
}
void GenerateReadInitCallbacks ()
string GetUniqueName (string uniqueGroup, object ob, string name)
{
- name = name.Replace ("[]","_array");
+ name = CodeIdentifier.MakeValid (name.Replace ("[]","_array"));
Hashtable names = (Hashtable) _uniqueNames [uniqueGroup];
if (names == null) {
names = new Hashtable ();
string GetUniqueClassName (string s)
{
- return classNames.AddUnique (s, null);
+ return classNames.AddUnique (CodeIdentifier.MakeValid (s), null);
}
string GetReadObjectCall (XmlTypeMapping typeMap, string isNullable, string checkType)
{
if (ob == null) return "null";
if (ob is string) return "\"" + ob.ToString().Replace("\"","\"\"") + "\"";
+ if (ob is DateTime) return "new DateTime (" + ((DateTime) ob).Ticks + ")";
+#if NET_2_0
+ if (ob is DateTimeOffset) return "new DateTimeOffset (" + ((DateTimeOffset) ob).Ticks + ")";
+#endif
+ if (ob is TimeSpan) return "new TimeSpan (" + ((TimeSpan) ob).Ticks + ")";
if (ob is bool) return ((bool)ob) ? "true" : "false";
if (ob is XmlQualifiedName) {
XmlQualifiedName qn = (XmlQualifiedName)ob;
public string WriterClassName;
public string WriteMethodName;
public string Namespace;
+#if NET_2_0
public string SerializerClassName;
+#endif
public string BaseSerializerClassName;
public string ImplementationClassName;
}