private void RetreiveElementValue(object o, MemberWrapper member, string elemValue, XElement xelemValue)
{
Type memberType = member.MemberType;
// when serializing collection with no containing element, then the real type attribute applies to the class
// containing the collection, not the collection itself. That's because the containing element of collection is not
// serialized. In this case the flag `isRealTypeAttributeNotRelevant` is set to true.
bool isRealTypeAttributeNotRelevant = member.CollectionAttributeInstance != null
&& member.CollectionAttributeInstance.SerializationType == YAXCollectionSerializationTypes.RecursiveWithNoContainingElement;
// try to retrieve the real-type if specified
if (xelemValue != null && !isRealTypeAttributeNotRelevant)
{
XAttribute realTypeAttribute = xelemValue.Attribute_NamespaceSafe(m_yaxLibNamespaceUri + m_trueTypeAttrName, m_documentDefaultNamespace);
if (realTypeAttribute != null)
{
Type realType = ReflectionUtils.GetTypeByName(realTypeAttribute.Value);
if (realType != null)
{
memberType = realType;
}
}
}
if (xelemValue != null && XMLUtils.IsElementCompletelyEmpty(xelemValue) &&
!ReflectionUtils.IsBasicType(memberType) && !member.IsTreatedAsCollection && !member.IsTreatedAsDictionary &&
!AtLeastOneOfMembersExists(xelemValue, memberType))
{
try
{
member.SetValue(o, member.DefaultValue);
}
catch
{
OnExceptionOccurred(new YAXDefaultValueCannotBeAssigned(member.Alias.LocalName, member.DefaultValue), member.TreatErrorsAs);
}
}
else if (memberType == typeof(string))
{
if (String.IsNullOrEmpty(elemValue) && xelemValue != null)
{
elemValue = xelemValue.IsEmpty ? null : String.Empty;
}
try
{
member.SetValue(o, elemValue);
}
catch
{
OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), m_defaultExceptionType);
}
}
else if (ReflectionUtils.IsBasicType(memberType))
{
object convertedObj;
if (ReflectionUtils.IsNullable(memberType) && String.IsNullOrEmpty(elemValue))
{
convertedObj = member.DefaultValue;
}
else
{
convertedObj = ReflectionUtils.ConvertBasicType(elemValue, memberType);
}
try
{
try
{
member.SetValue(o, convertedObj);
}
catch
{
OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), m_defaultExceptionType);
}
}
catch (Exception ex)
{
if (ex is YAXException)
{
throw;
}
OnExceptionOccurred(new YAXBadlyFormedInput(member.Alias.LocalName, elemValue), member.TreatErrorsAs);
try
{
member.SetValue(o, member.DefaultValue);
}
catch
{
OnExceptionOccurred(new YAXDefaultValueCannotBeAssigned(member.Alias.LocalName, member.DefaultValue), m_defaultExceptionType);
}
}
}
else if (member.IsTreatedAsDictionary && member.DictionaryAttributeInstance != null)
{
DeserializeTaggedDictionaryMember(o, member, xelemValue);
}
else if (member.IsTreatedAsCollection)
{
DeserializeCollectionMember(o, member, memberType, elemValue, xelemValue);
}
else
{
var namespaceToOverride = member.Namespace.IfEmptyThen(TypeNamespace).IfEmptyThenNone();
var ser = NewInternalSerializer(memberType, namespaceToOverride, null);
ser.IsCraetedToDeserializeANonCollectionMember = !(member.IsTreatedAsDictionary || member.IsTreatedAsCollection);
if (m_desObject != null) // i.e. it is in resuming mode
ser.SetDeserializationBaseObject(member.GetValue(o));
object convertedObj = ser.DeserializeBase(xelemValue);
FinalizeNewSerializer(ser, false);
try
{
member.SetValue(o, convertedObj);
}
catch
{
OnExceptionOccurred(new YAXPropertyCannotBeAssignedTo(member.Alias.LocalName), m_defaultExceptionType);
}
}
}