Exemplo n.º 1
0
        private Result ParseResult(ChecklistDeserializationDto.Result dto, Result parent = null, int depth = 0)
        {
            var targetType = depth == 0 ? typeof(RubricResult) :
                             dto.Children?.Any() ?? false ? typeof(GroupResult) :
                             typeof(PointResult);
            var targetInstance = (Result)FormatterServices.GetUninitializedObject(targetType);

            SetPropertyValueViaBackingField(targetType, nameof(Result.ConjunctElementCode), targetInstance, dto.ConjunctElementCode);
            SetPropertyValueViaBackingField(targetType, nameof(Result.ElementCode), targetInstance, dto.ElementCode);
            SetPropertyValueViaBackingField(targetType, nameof(Result.Name), targetInstance, dto.Name);
            SetPropertyValueViaBackingField(targetType, nameof(Result.ShortName), targetInstance, dto.ShortName);
            SetPropertyValueViaBackingField(targetType, nameof(Result.InspectorComment), targetInstance, dto.InspectorComment);
            SetPropertyValueViaBackingField(targetType, nameof(Result.FarmerComment), targetInstance, dto.FarmerComment);
            SetPropertyValueViaBackingField(targetType, nameof(Result.Outcome), targetInstance, Parse(dto.Outcome));
            SetPropertyValueViaBackingField(targetType, nameof(Result.IsAutoSet), targetInstance, dto.IsAutoSet);
            SetPropertyValueViaBackingField(targetType, nameof(Result.Defect), targetInstance, Parse(dto.Defect));
            SetPropertyValueViaBackingField(targetType, nameof(Result.Seriousness), targetInstance, Parse(dto.Seriousness));
            // todo PredefinedDefect

            SetPropertyValueViaBackingField(targetType, nameof(Result.Children), targetInstance, new SortedList <string, ITreeNode <Result> >());
            if (dto.Children?.Any() == true)
            {
                foreach (var child in dto.Children)
                {
                    targetInstance.Children.TryAdd(child.Key, ParseResult(child.Value, targetInstance, ++depth));
                }
            }

            SetPropertyValueViaBackingField(targetType, nameof(Result.Parent), targetInstance, parent);
            return(targetInstance);
        }
Exemplo n.º 2
0
        private static Result ParseResult(ChecklistDeserializationDto.Result dto, Result parent = null, int depth = 0)
        {
            var targetType = depth == 0 ? typeof(RubricResult) :
                             dto.Children.Any() ? typeof(GroupResult) :
                             typeof(PointResult);
            var targetInstance = (Result)FormatterServices.GetUninitializedObject(targetType);

            foreach (var sourceProp in SourceProperties)
            {
                string indentation = "".PadLeft(depth * 2);
                Console.WriteLine($"{indentation}Trying to map source property {sourceProp.Name}...");
                if (!TargetProperties.TryGetValue(sourceProp.Name, out var targetProp))
                {
                    continue;
                }

                var value = sourceProp.GetValue(dto);
                Console.WriteLine($"{indentation}...to target property {targetProp.Name} with value {value}.");
                SetPropertyValueViaBackingField(typeof(Result), targetProp.Name, targetInstance, value); // does not seem to work with inherited properties
            }

            if (dto.Children.Any() && targetInstance.Children == null)
            {
                SetPropertyValueViaBackingField(nameof(Result.Children), targetInstance, new SortedList <string, ITreeNode <Result> >());
            }

            foreach (var child in dto.Children)
            {
                targetInstance.Children.TryAdd(child.Key, ParseResult(child.Value, (Result)targetInstance, ++depth));
            }

            SetPropertyValueViaBackingField(nameof(Result.Parent), targetInstance, parent);
            return(targetInstance);
        }