private void WriteStructMethod(StructMapping mapping, string n, string ns, object o, bool isNullable, bool needType, XmlMapping parentMapping = null)
{
if (mapping.IsSoap && mapping.TypeDesc.IsRoot)
{
return;
}
if (mapping.IsSoap)
{
}
else
{
if (o == null)
{
if (isNullable)
{
WriteNullTagLiteral(n, ns);
}
return;
}
if (!needType &&
o.GetType() != mapping.TypeDesc.Type)
{
if (WriteDerivedTypes(mapping, n, ns, o, isNullable))
{
return;
}
if (mapping.TypeDesc.IsRoot)
{
if (WriteEnumAndArrayTypes(mapping, o, n, ns, parentMapping))
{
return;
}
WriteTypedPrimitive(n, ns, o, true);
return;
}
throw CreateUnknownTypeException(o);
}
}
if (!mapping.TypeDesc.IsAbstract)
{
if (mapping.TypeDesc.Type != null && typeof(XmlSchemaObject).IsAssignableFrom(mapping.TypeDesc.Type))
{
EscapeName = false;
}
XmlSerializerNamespaces xmlnsSource = null;
MemberMapping[] members = TypeScope.GetAllMembers(mapping);
int xmlnsMember = FindXmlnsIndex(members);
if (xmlnsMember >= 0)
{
MemberMapping member = members[xmlnsMember];
xmlnsSource = (XmlSerializerNamespaces)GetMemberValue(o, member.Name);
}
if (!mapping.IsSoap)
{
WriteStartElement(n, ns, o, false, xmlnsSource);
if (!mapping.TypeDesc.IsRoot)
{
if (needType)
{
WriteXsiType(mapping.TypeName, mapping.Namespace);
}
}
}
else if (xmlnsSource != null)
{
WriteNamespaceDeclarations(xmlnsSource);
}
for (int i = 0; i < members.Length; i++)
{
MemberMapping m = members[i];
string memberName = m.Name;
object memberValue = GetMemberValue(o, memberName);
bool isSpecified = true;
bool shouldPersist = true;
if (m.CheckSpecified != SpecifiedAccessor.None)
{
string specifiedMemberName = m.Name + "Specified";
isSpecified = (bool)GetMemberValue(o, specifiedMemberName);
}
if (m.CheckShouldPersist)
{
string methodInvoke = "ShouldSerialize" + m.Name;
MethodInfo method = o.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke);
shouldPersist = (bool)method.Invoke(o, Array.Empty <object>());
}
if (m.Attribute != null)
{
if (isSpecified && shouldPersist)
{
WriteMember(memberValue, m.Attribute, m.TypeDesc, o);
}
}
}
for (int i = 0; i < members.Length; i++)
{
MemberMapping m = members[i];
string memberName = m.Name;
object memberValue = GetMemberValue(o, memberName);
bool isSpecified = true;
bool shouldPersist = true;
if (m.CheckSpecified != SpecifiedAccessor.None)
{
string specifiedMemberName = m.Name + "Specified";
isSpecified = (bool)GetMemberValue(o, specifiedMemberName);
}
if (m.CheckShouldPersist)
{
string methodInvoke = "ShouldSerialize" + m.Name;
MethodInfo method = o.GetType().GetTypeInfo().GetDeclaredMethod(methodInvoke);
shouldPersist = (bool)method.Invoke(o, Array.Empty <object>());
}
if (m.Xmlns != null)
{
continue;
}
bool checkShouldPersist = m.CheckShouldPersist && (m.Elements.Length > 0 || m.Text != null);
if (!checkShouldPersist)
{
shouldPersist = true;
}
object choiceSource = null;
if (m.ChoiceIdentifier != null)
{
choiceSource = GetMemberValue(o, m.ChoiceIdentifier.MemberName);
}
if (isSpecified && shouldPersist)
{
WriteMember(memberValue, choiceSource, m.ElementsSortedByDerivation, m.Text, m.ChoiceIdentifier, m.TypeDesc, true, parentMapping);
}
}
if (!mapping.IsSoap)
{
WriteEndElement(o);
}
}
}