private static IPrintToken CreateInternal([CanBeNull] object item, [NotNull] HashSet <object> path, [NotNull] PrintSettings settings)
        {
            if (item == null)
            {
                return(NullValue);
            }

            if (!path.Add(item))
            {
                return(CyclicValue);
            }

            try
            {
                var itemType = item.GetType();

                using (SecurityHelper.StartSecurityScope(itemType))
                {
                    if (ToStringDetector.HasCustomToString(itemType))
                    {
                        return(new ValueToken(item.ToString()));
                    }

                    if (CustomFormatters.TryFormat(item, out var customFormatting))
                    {
                        return(new ValueToken(customFormatting));
                    }

                    if (DictionaryInspector.IsSimpleDictionary(itemType))
                    {
                        var pairs  = DictionaryInspector.EnumerateSimpleDictionary(item);
                        var tokens = pairs.Select(pair => new PropertyToken(pair.Item1, CreateInternal(pair.Item2, path, settings))).ToArray();
                        if (tokens.Length == 0)
                        {
                            return(EmptyObjectValue);
                        }

                        return(new ObjectToken(tokens));
                    }

                    if (item is IEnumerable sequence)
                    {
                        if (!sequence.GetEnumerator().MoveNext())
                        {
                            return(EmptySequenceValue);
                        }

                        var tokens = new List <IPrintToken>();

                        foreach (var element in sequence)
                        {
                            tokens.Add(CreateInternal(element, path, settings));
                        }

                        return(new SequenceToken(tokens));
                    }

                    var fieldsAndProperties = new List <PropertyToken>();

                    foreach (var field in itemType.GetInstanceFields())
                    {
                        fieldsAndProperties.Add(ConstructProperty(field, () => CreateInternal(field.GetValue(item), path, settings), settings));
                    }

                    foreach (var property in itemType.GetInstanceProperties())
                    {
                        fieldsAndProperties.Add(ConstructProperty(property, () => CreateInternal(property.GetValue(item), path, settings), settings));
                    }

                    if (fieldsAndProperties.Count == 0)
                    {
                        return(EmptyObjectValue);
                    }

                    return(new ObjectToken(fieldsAndProperties));
                }
            }
            finally
            {
                path.Remove(item);
            }
        }
 public static IPrintToken Create([CanBeNull] object item, [NotNull] PrintSettings settings)
 => CreateInternal(item, new HashSet <object>(ByReferenceEqualityComparer <object> .Instance), settings);
        private static PropertyToken ConstructProperty(MemberInfo member, Func <IPrintToken> getValue, PrintSettings settings)
        {
            IPrintToken value;

            if (settings.HideSecretValues && SecurityHelper.IsSecret(member))
            {
                value = SecretValue;
            }
            else
            {
                try
                {
                    value = getValue();
                }
                catch
                {
                    value = ErrorValue;
                }
            }

            return(new PropertyToken(member.Name, value));
        }
        private static PropertyToken ConstructPropertyToken(bool shouldHide, Func <IPrintToken> valueProvider, string propertyName, PrintSettings settings)
        {
            IPrintToken value;

            if (settings.HideSecretValues && shouldHide)
            {
                value = SecretValue;
            }
            else
            {
                try
                {
                    value = valueProvider();
                }
                catch
                {
                    value = ErrorValue;
                }
            }

            return(new PropertyToken(propertyName, value));
        }
 private static PropertyToken ConstructProperty(MemberInfo member, Func <IPrintToken> getValue, PrintSettings settings) =>
 ConstructPropertyToken(SecurityHelper.IsSecret(member), getValue, member.Name, settings);
Beispiel #6
0
 public PrintContext(PrintSettings settings)
 => this.settings = settings;