/// <summary> /// Gets the member value. /// </summary> /// <param name="model">The model.</param> /// <param name="memberName">Name of the member.</param> /// <param name="modelInfo">The model information.</param> /// <returns>MemberValue.</returns> public virtual MemberValue GetMemberValue(object model, string memberName, SerializationModelInfo modelInfo) { var modelType = model.GetType(); try { object value = null; var modelEditor = model as IModelEditor; if (modelEditor != null && modelInfo.CatelPropertyNames.Contains(memberName)) { var memberMetadata = modelInfo.CatelPropertiesByName[memberName]; if (_objectAdapter.GetMemberValue(model, memberName, out value)) { var propertyValue = new MemberValue(SerializationMemberGroup.CatelProperty, modelType, memberMetadata.MemberType, memberMetadata.MemberName, memberMetadata.MemberNameForSerialization, value); return(propertyValue); } } if (modelInfo.PropertiesByName.TryGetValue(memberName, out var propertyMemberMetadata)) { if (_objectAdapter.GetMemberValue(model, memberName, out value)) { var propertyValue = new MemberValue(SerializationMemberGroup.RegularProperty, modelType, propertyMemberMetadata.MemberType, propertyMemberMetadata.MemberName, propertyMemberMetadata.MemberNameForSerialization, value); return(propertyValue); } } if (modelInfo.FieldsByName.TryGetValue(memberName, out var fieldMemberMetadata)) { if (_objectAdapter.GetMemberValue(model, memberName, out value)) { var fieldValue = new MemberValue(SerializationMemberGroup.Field, modelType, fieldMemberMetadata.MemberType, fieldMemberMetadata.MemberName, fieldMemberMetadata.MemberNameForSerialization, value); return(fieldValue); } } } catch (Exception ex) { Log.Warning(ex, "Failed to get value of member '{0}.{1}', skipping item during serialization", modelType.GetSafeFullName(false), memberName); } return(null); }
/// <summary> /// Sets the member value. /// </summary> /// <param name="model">The model.</param> /// <param name="member">The member.</param> /// <param name="modelInfo">The model information.</param> public virtual void SetMemberValue(object model, MemberValue member, SerializationModelInfo modelInfo) { var modelType = model.GetType(); try { var finalValue = BoxingCache.GetBoxedValue(member.Value); // In this very special occasion, we will not use ObjectAdapter since it // will cause property change notifications (which we don't want during deserialization) var modelEditor = model as IModelEditor; if (modelEditor != null && modelInfo.CatelPropertyNames.Contains(member.Name)) { modelEditor.SetValueFastButUnsecure(member.Name, finalValue); return; } _objectAdapter.SetMemberValue(model, member.Name, finalValue); } catch (Exception ex) { Log.Warning(ex, $"Failed to populate '{modelType.GetSafeFullName(false)}.{member.Name}', setting the member value threw an exception"); } }
/// <summary> /// Gets the member value. /// </summary> /// <param name="model">The model.</param> /// <param name="memberName">Name of the member.</param> /// <param name="modelInfo">The model information.</param> /// <returns>MemberValue.</returns> public virtual MemberValue GetMemberValue(object model, string memberName, SerializationModelInfo modelInfo) { var modelType = model.GetType(); try { var modelEditor = model as IModelEditor; if (modelEditor != null && modelInfo.CatelPropertyNames.Contains(memberName)) { var memberMetadata = modelInfo.CatelPropertiesByName[memberName]; var actualPropertyValue = modelEditor.GetValueFastButUnsecure(memberName); var propertyValue = new MemberValue(SerializationMemberGroup.CatelProperty, modelType, memberMetadata.MemberType, memberMetadata.MemberName, memberMetadata.MemberNameForSerialization, actualPropertyValue); return propertyValue; } if (modelInfo.PropertiesByName.ContainsKey(memberName)) { var memberMetadata = modelInfo.PropertiesByName[memberName]; object value = null; var get = false; var propertySerializable = model as IPropertySerializable; if (propertySerializable != null) { get = propertySerializable.GetPropertyValue(memberName, ref value); } if (!get) { value = ((PropertyInfo)memberMetadata.Tag).GetValue(model, null); } var propertyValue = new MemberValue(SerializationMemberGroup.RegularProperty, modelType, memberMetadata.MemberType, memberMetadata.MemberName, memberMetadata.MemberNameForSerialization, value); return propertyValue; } if (modelInfo.FieldsByName.ContainsKey(memberName)) { var memberMetadata = modelInfo.FieldsByName[memberName]; object value = null; var get = false; var fieldSerializable = model as IFieldSerializable; if (fieldSerializable != null) { get = fieldSerializable.GetFieldValue(memberName, ref value); } if (!get) { value = ((FieldInfo)memberMetadata.Tag).GetValue(model); } var fieldValue = new MemberValue(SerializationMemberGroup.Field, modelType, memberMetadata.MemberType, memberMetadata.MemberName, memberMetadata.MemberNameForSerialization, value); return fieldValue; } } catch (Exception ex) { Log.Warning(ex, "Failed to get value of member '{0}.{1}', skipping item during serialization", modelType.GetSafeFullName(false), memberName); } return null; }
/// <summary> /// Sets the member value. /// </summary> /// <param name="model">The model.</param> /// <param name="member">The member.</param> /// <param name="modelInfo">The model information.</param> public virtual void SetMemberValue(object model, MemberValue member, SerializationModelInfo modelInfo) { var modelType = model.GetType(); try { var modelEditor = model as IModelEditor; if (modelEditor != null && modelInfo.CatelPropertyNames.Contains(member.Name)) { modelEditor.SetValueFastButUnsecure(member.Name, member.Value); } else if (modelInfo.PropertyNames.Contains(member.Name)) { var set = false; var propertySerializable = model as IPropertySerializable; if (propertySerializable != null) { set = propertySerializable.SetPropertyValue(member.Name, member.Value); } if (!set) { var memberMetadata = modelInfo.PropertiesByName[member.Name]; if (memberMetadata != null) { ((PropertyInfo)memberMetadata.Tag).SetValue(model, member.Value, null); set = true; } } if (!set) { Log.Warning("Failed to set property '{0}.{1}' because the member cannot be found on the model", modelType.GetSafeFullName(false), member.Name); } } else if (modelInfo.FieldNames.Contains(member.Name)) { var set = false; var fieldSerializable = model as IFieldSerializable; if (fieldSerializable != null) { set = fieldSerializable.SetFieldValue(member.Name, member.Value); } if (!set) { var memberMetadata = modelInfo.FieldsByName[member.Name]; if (memberMetadata != null) { ((FieldInfo)memberMetadata.Tag).SetValue(model, member.Value); set = true; } } if (!set) { Log.Warning("Failed to set field '{0}.{1}' because the member cannot be found on the model", modelType.GetSafeFullName(false), member.Name); } } } catch (Exception ex) { Log.Warning(ex, "Failed to populate '{0}.{1}', setting the member value threw an exception", modelType.GetSafeFullName(false), member.Name); } }
/// <summary> /// Gets the member value. /// </summary> /// <param name="model">The model.</param> /// <param name="memberName">Name of the member.</param> /// <param name="modelInfo">The model information.</param> /// <returns>MemberValue.</returns> public virtual MemberValue GetMemberValue(object model, string memberName, SerializationModelInfo modelInfo) { var modelType = model.GetType(); try { var modelEditor = model as IModelEditor; if (modelEditor != null && modelInfo.CatelPropertyNames.Contains(memberName)) { var memberMetadata = modelInfo.CatelPropertiesByName[memberName]; var actualPropertyValue = modelEditor.GetValueFastButUnsecure(memberName); var propertyValue = new MemberValue(SerializationMemberGroup.CatelProperty, modelType, memberMetadata.MemberType, memberMetadata.MemberName, memberMetadata.MemberNameForSerialization, actualPropertyValue); return(propertyValue); } if (modelInfo.PropertiesByName.ContainsKey(memberName)) { var memberMetadata = modelInfo.PropertiesByName[memberName]; object value = null; var get = false; var propertySerializable = model as IPropertySerializable; if (propertySerializable != null) { get = propertySerializable.GetPropertyValue(memberName, ref value); } if (!get) { value = ((PropertyInfo)memberMetadata.Tag).GetValue(model, null); } var propertyValue = new MemberValue(SerializationMemberGroup.RegularProperty, modelType, memberMetadata.MemberType, memberMetadata.MemberName, memberMetadata.MemberNameForSerialization, value); return(propertyValue); } if (modelInfo.FieldsByName.ContainsKey(memberName)) { var memberMetadata = modelInfo.FieldsByName[memberName]; object value = null; var get = false; var fieldSerializable = model as IFieldSerializable; if (fieldSerializable != null) { get = fieldSerializable.GetFieldValue(memberName, ref value); } if (!get) { value = ((FieldInfo)memberMetadata.Tag).GetValue(model); } var fieldValue = new MemberValue(SerializationMemberGroup.Field, modelType, memberMetadata.MemberType, memberMetadata.MemberName, memberMetadata.MemberNameForSerialization, value); return(fieldValue); } } catch (Exception ex) { Log.Warning(ex, "Failed to get value of member '{0}.{1}', skipping item during serialization", modelType.GetSafeFullName(false), memberName); } return(null); }