/// <summary> /// Gets the type of the member. /// </summary> /// <param name="modelType">Type of the model.</param> /// <param name="memberName">Name of the member.</param> /// <returns>The <see cref="Type"/> of the member.</returns> protected Type GetMemberType(Type modelType, string memberName) { var catelProperties = SerializationManager.GetCatelProperties(modelType); if (catelProperties.ContainsKey(memberName)) { return(catelProperties[memberName].MemberType); } var regularProperties = SerializationManager.GetRegularProperties(modelType); if (regularProperties.ContainsKey(memberName)) { return(regularProperties[memberName].MemberType); } var fields = SerializationManager.GetFields(modelType); if (fields.ContainsKey(memberName)) { return(fields[memberName].MemberType); } return(null); }
/// <summary> /// Gets the type of the member. /// </summary> /// <param name="modelType">Type of the model.</param> /// <param name="memberName">Name of the member.</param> /// <returns>The <see cref="Type"/> of the member.</returns> protected Type GetMemberType(Type modelType, string memberName) { var catelProperties = SerializationManager.GetCatelProperties(modelType); if (catelProperties.TryGetValue(memberName, out var catelProperty)) { return(catelProperty.MemberType); } var regularProperties = SerializationManager.GetRegularProperties(modelType); if (regularProperties.TryGetValue(memberName, out var regularProperty)) { return(regularProperty.MemberType); } var fields = SerializationManager.GetFields(modelType); if (fields.TryGetValue(memberName, out var field)) { return(field.MemberType);; } return(null); }
/// <summary> /// Populates the model with the specified members. /// </summary> /// <param name="model">The model.</param> /// <param name="members">The members.</param> /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="members"/> is <c>null</c>.</exception> protected virtual void PopulateModel(object model, params MemberValue[] members) { Argument.IsNotNull("model", model); Argument.IsNotNull("properties", members); var modelType = model.GetType(); var modelInfo = _serializationModelCache.GetFromCacheOrFetch(modelType, () => { var catelProperties = SerializationManager.GetCatelProperties(modelType); var fields = SerializationManager.GetFields(modelType); var regularProperties = SerializationManager.GetRegularProperties(modelType); return(new SerializationModelInfo(modelType, catelProperties, fields, regularProperties)); }); foreach (var member in members) { ObjectAdapter.SetMemberValue(model, member, modelInfo); } }
public void ReturnsCorrectValue() { var serializationManager = new SerializationManager(); var fields = serializationManager.GetFields(typeof(TestModel)).ToArray(); Assert.AreEqual(1, fields.Length); Assert.AreEqual("_includedField", fields[0].Key); Assert.AreEqual(SerializationMemberGroup.Field, fields[0].Value.MemberGroup); }
public void ThrowsArgumentNullExceptionForNullType() { var serializationManager = new SerializationManager(); ExceptionTester.CallMethodAndExpectException<ArgumentNullException>(() => serializationManager.GetFields(null)); }
/// <summary> /// Populates the model with the specified members. /// </summary> /// <param name="context">The serialization context.</param> /// <param name="members">The members.</param> /// <exception cref="ArgumentNullException">The <paramref name="context"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">The <paramref name="members"/> is <c>null</c>.</exception> protected virtual void PopulateModel(ISerializationContext <TSerializationContextInfo> context, List <MemberValue> members) { Argument.IsNotNull("model", context); Argument.IsNotNull("members", members); if (members.Count > 0) { var firstMember = members[0]; if (firstMember.MemberGroup == SerializationMemberGroup.SimpleRootObject) { // Completely replace root object (this is a basic (non-reference) type) context.Model = firstMember.Value; } else if (firstMember.MemberGroup == SerializationMemberGroup.Dictionary) { var targetDictionary = context.Model as IDictionary; if (targetDictionary is null) { throw Log.ErrorAndCreateException <NotSupportedException>("'{0}' seems to be a dictionary, but target model cannot be updated because it does not implement IDictionary", context.ModelTypeName); } targetDictionary.Clear(); var sourceDictionary = firstMember.Value as IDictionary; if (sourceDictionary != null) { foreach (var key in sourceDictionary.Keys) { targetDictionary.Add(key, sourceDictionary[key]); } } } else if (firstMember.MemberGroup == SerializationMemberGroup.Collection) { if (context.ModelType.IsArrayEx()) { context.Model = firstMember.Value; } else { var targetCollection = context.Model as IList; if (targetCollection is null) { throw Log.ErrorAndCreateException <NotSupportedException>("'{0}' seems to be a collection, but target model cannot be updated because it does not implement IList", context.ModelTypeName); } targetCollection.Clear(); var sourceCollection = firstMember.Value as IEnumerable; if (sourceCollection != null) { foreach (var item in sourceCollection) { targetCollection.Add(item); } } } } else { // Populate using properties var model = context.Model; var modelType = context.ModelType; var modelInfo = _serializationModelCache.GetFromCacheOrFetch(modelType, () => { var catelProperties = SerializationManager.GetCatelProperties(modelType); var fields = SerializationManager.GetFields(modelType); var regularProperties = SerializationManager.GetRegularProperties(modelType); return(new SerializationModelInfo(modelType, catelProperties, fields, regularProperties)); }); foreach (var member in members) { ObjectAdapter.SetMemberValue(model, member, modelInfo); } } } }