Example #1
0
        /// <summary>
        /// Gets the serializable members for the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="membersToIgnore">The members to ignore.</param>
        /// <returns>The list of members to serialize.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public virtual List <MemberValue> GetSerializableMembers(ModelBase model, params string[] membersToIgnore)
        {
            Argument.IsNotNull("model", model);

            var membersToIgnoreHashSet = new HashSet <string>(membersToIgnore);

            var modelType     = model.GetType();
            var catelTypeInfo = PropertyDataManager.Default.GetCatelTypeInfo(modelType);

            var catelPropertyNames    = SerializationManager.GetCatelPropertyNames(modelType);
            var fieldsToSerialize     = SerializationManager.GetFieldsToSerialize(modelType);
            var propertiesToSerialize = SerializationManager.GetPropertiesToSerialize(modelType);

            var listToSerialize    = new List <MemberValue>();
            var checkedMemberNames = new List <string>();

            foreach (var fieldToSerialize in fieldsToSerialize)
            {
                checkedMemberNames.Add(fieldToSerialize);

                if (membersToIgnoreHashSet.Contains(fieldToSerialize) || ShouldIgnoreMember(model, fieldToSerialize))
                {
                    Log.Debug("Field '{0}' is being ignored for serialization", fieldToSerialize);
                    continue;
                }

                try
                {
                    Log.Debug("Adding field '{0}' to list of objects to serialize", fieldToSerialize);

                    var fieldInfo  = modelType.GetFieldEx(fieldToSerialize);
                    var fieldValue = new MemberValue(SerializationMemberGroup.Field, modelType, fieldInfo.FieldType, fieldInfo.Name, fieldInfo.GetValue(model));

                    listToSerialize.Add(fieldValue);
                }
                catch (Exception ex)
                {
                    Log.Warning(ex, "Failed to get value of member '{0}.{1}', skipping item during serialization", modelType.GetSafeFullName(), fieldToSerialize);
                }
            }

            foreach (var propertyToSerialize in propertiesToSerialize)
            {
                checkedMemberNames.Add(propertyToSerialize);

                if (membersToIgnoreHashSet.Contains(propertyToSerialize) || ShouldIgnoreMember(model, propertyToSerialize))
                {
                    Log.Debug("Property '{0}' is being ignored for serialization", propertyToSerialize);
                    continue;
                }

                try
                {
                    Log.Debug("Adding property '{0}' to list of objects to serialize", propertyToSerialize);

                    if (catelPropertyNames.Contains(propertyToSerialize))
                    {
                        var propertyData        = catelTypeInfo.GetPropertyData(propertyToSerialize);
                        var actualPropertyValue = model.GetValueFast(propertyToSerialize);
                        var propertyValue       = new MemberValue(SerializationMemberGroup.CatelProperty, modelType, propertyData.Type, propertyData.Name, actualPropertyValue);

                        listToSerialize.Add(propertyValue);
                    }
                    else
                    {
                        var propertyInfo  = modelType.GetPropertyEx(propertyToSerialize);
                        var propertyValue = new MemberValue(SerializationMemberGroup.RegularProperty, modelType, propertyInfo.PropertyType, propertyInfo.Name, propertyInfo.GetValue(model, null));

                        listToSerialize.Add(propertyValue);
                    }
                }
                catch (Exception ex)
                {
                    Log.Warning(ex, "Failed to get value of member '{0}.{1}', skipping item during serialization", modelType.GetSafeFullName(), propertyToSerialize);
                }
            }

            return(listToSerialize);
        }