private object DeserializeNodeToObject(XElement objectNode, bool ignoreError, XmlDeserializeContext context) { object data = null; int objID = objectNode.Attribute("id", 0); if (context.ObjectContext.TryGetValue(objID, out data) == false) { try { Type type = context.GetTypeInfo(objectNode.AttributeValueWithAlterName("ownerTypeID", "oti")); XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(type); if (attr != null && attr.IgnoreDeserializeError == true) { ignoreError = attr.IgnoreDeserializeError; } data = CreateSerializableInstance(type); context.ObjectContext.Add(objID, data); if ((data is IXElementSerializable) && objectNode.Attribute("cs", false) == true) { ((IXElementSerializable)data).Deserialize(objectNode, context); } else { string objectValue = objectNode.Element("Value", string.Empty); if (objectValue.IsNotEmpty()) { SerializationBinder binder = this.Binder ?? UnknownTypeStrategyBinder.Instance; data = SerializationHelper.DeserializeStringToObject(objectValue, SerializationFormatterType.Binary, binder); } else { DeserializeNodesToProperties(objectNode, data, context); } } if (data is IXmlDeserialize) { ((IXmlDeserialize)data).AfterDeserialize(context); } } catch (System.Exception) { if (ignoreError == false) { throw; } } } return(data); }
/// <summary> /// /// </summary> /// <param name="fieldInfo"></param> public ExtendedFieldInfo(FieldInfo fieldInfo) { MemberInfo declaringInfo = fieldInfo; if (fieldInfo.IsDefined(typeof(CompilerGeneratedAttribute), false)) { declaringInfo = GetFieldOriginalMemberInfo(fieldInfo); } XElementFieldSerializeAttribute fieldAttr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(declaringInfo); if (fieldAttr != null) { this._AlternateFieldName = fieldAttr.AlternateFieldName; this._IgnoreDeserializeError = fieldAttr.IgnoreDeserializeError; } this._FieldInfo = fieldInfo; }
private void DeserializeNodesToProperties(XElement parent, object graph, XmlDeserializeContext context) { TypeFields tf = TypeFields.GetTypeFields(graph.GetType()); foreach (KeyValuePair <TypeFieldInfo, ExtendedFieldInfo> kp in tf.Fields) { ExtendedFieldInfo efi = kp.Value; if (efi.IsNotSerialized == false && OnFieldCanXElementSerialize(efi)) { System.Type realType = efi.FieldInfo.FieldType; var propertiesElement = from property in parent.Descendants("F") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; if (propertiesElement.FirstOrDefault() == null) { propertiesElement = from property in parent.Descendants("Field") where (property.AttributeWithAlterName("name", "n", string.Empty) == efi.AlternateFieldName || property.AttributeWithAlterName("name", "n", string.Empty) == efi.FieldInfo.Name) && context.TypeContext[property.AttributeWithAlterName("ownerTypeID", "oti", -1)] == kp.Key.ObjectType select property; } XElement propertyElement = propertiesElement.FirstOrDefault(); if (propertyElement != null) { object data = null; if (propertyElement.AttributeWithAlterName("isRef", "r", false) == true) { XElement objectElement = GetObjectElementByID(context.RootElement, propertyElement.AttributeWithAlterName("value", "v", 0)); if (objectElement != null) { bool ignoreError = propertyElement.Attribute("ide", false); if (ignoreError == false) { XElementFieldSerializeAttribute attr = AttributeHelper.GetCustomAttribute <XElementFieldSerializeAttribute>(efi.FieldInfo); if (attr != null) { ignoreError = attr.IgnoreDeserializeError; } } data = DeserializeNodeToObject(objectElement, ignoreError, context); SetValueToObject(efi.FieldInfo, graph, data); } } else { data = propertyElement.AttributeWithAlterName("value", "v", TypeCreator.GetTypeDefaultValue(realType)); if (Convertible(realType, data)) { SetValueToObject(efi.FieldInfo, graph, ConvertData(efi.FieldInfo, data)); } } } } } DeserializeXmlSerilizableList(parent, graph, context); }