void GenerateReadClassInstance (XmlTypeMapping typeMap, string isNullable, string checkType)
{
SetHookVar ("$OBJECT", "ob");
if (!typeMap.TypeData.IsValueType)
{
WriteLine (typeMap.TypeData.CSharpFullName + " ob = null;");
if (GenerateReadHook (HookType.type, typeMap.TypeData.Type)) {
WriteLine ("return ob;");
return;
}
if (_format == SerializationFormat.Literal) {
WriteLine ("if (" + isNullable + " && ReadNull()) return null;");
WriteLine ("");
WriteLine ("if (checkType) ");
WriteLineInd ("{");
}
else {
WriteLine ("if (ReadNull()) return null;");
WriteLine ("");
}
}
else
{
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;");
return;
}
}
WriteLine ("System.Xml.XmlQualifiedName t = GetXsiType();");
WriteLine ("if (t == null)");
if (typeMap.TypeData.Type != typeof(object))
WriteLine ("{ }");
else
WriteLine ("\treturn " + GetCast (typeMap.TypeData, "ReadTypedPrimitive (new System.Xml.XmlQualifiedName(\"anyType\", System.Xml.Schema.XmlSchema.Namespace))") + ";");
foreach (XmlTypeMapping realMap in typeMap.DerivedTypes)
{
WriteLineInd ("else if (t.Name == " + GetLiteral (realMap.XmlType) + " && t.Namespace == " + GetLiteral (realMap.XmlTypeNamespace) + ")");
WriteLine ("return " + GetReadObjectCall(realMap, isNullable, checkType) + ";");
Unindent ();
}
WriteLine ("else if (t.Name != " + GetLiteral (typeMap.XmlType) + " || t.Namespace != " + GetLiteral (typeMap.XmlTypeNamespace) + ")");
if (typeMap.TypeData.Type == typeof(object))
WriteLine ("\treturn " + GetCast (typeMap.TypeData, "ReadTypedPrimitive (t)") + ";");
else
WriteLine ("\tthrow CreateUnknownTypeException(t);");
if (!typeMap.TypeData.IsValueType)
{
if (_format == SerializationFormat.Literal)
WriteLineUni ("}");
if (typeMap.TypeData.Type.IsAbstract) {
GenerateEndHook ();
WriteLine ("return ob;");
return;
}
WriteLine ("");
WriteLine (String.Format ("ob = ({0}) Activator.CreateInstance(typeof({0}), true);", typeMap.TypeData.CSharpFullName));
}
WriteLine ("");
WriteLine ("Reader.MoveToElement();");
WriteLine ("");
GenerateReadMembers (typeMap, (ClassMap)typeMap.ObjectMap, "ob", false, false);
WriteLine ("");
GenerateEndHook ();
WriteLine ("return ob;");
}