コード例 #1
0
        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))
                {
                    var isSecretType = settings.HideSecretValues && SecurityHelper.IsSecret(itemType);

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

                    var isNode = typeof(ISettingsNode).IsAssignableFrom(itemType);
                    if (!isSecretType && (ParseMethodFinder.HasAnyKindOfParseMethod(itemType) || isNode) && ToStringDetector.TryInvokeCustomToString(itemType, item, out var asString))
                    {
                        return(new ValueToken(asString));
                    }

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

                        return(new ObjectToken(tokens));
                    }

                    if (isSecretType)
                    {
                        return(SecretValue);
                    }

                    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);
            }
        }
コード例 #2
0
        private static ISettingsNode ParseObject([CanBeNull] string name, [CanBeNull] object item, [NotNull] HashSet <object> path, [NotNull] ObjectSourceSettings settings)
        {
            if (item == null)
            {
                return(new ValueNode(name, null));
            }

            if (!path.Add(item))
            {
                throw new ArgumentException("Object has cyclic dependency.");
            }

            try
            {
                var itemType = item.GetType();

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

                if (ParseMethodFinder.HasAnyKindOfParseMethod(itemType) && ToStringDetector.TryInvokeCustomToString(itemType, item, out var asString))
                {
                    return(new ValueNode(name, asString));
                }

                if (DictionaryInspector.IsSimpleDictionary(itemType))
                {
                    return(ParseDictionary(name, DictionaryInspector.EnumerateSimpleDictionary(item), path, settings));
                }

                if (item is IEnumerable sequence)
                {
                    return(ParseEnumerable(name, sequence, path, settings));
                }

                var fieldsAndProperties = new List <ISettingsNode>();

                foreach (var field in itemType.GetInstanceFields())
                {
                    var fieldValue = field.GetValue(item);
                    if (fieldValue != null || !settings.IgnoreFieldsWithNullValue)
                    {
                        fieldsAndProperties.Add(ParseObject(field.Name, fieldValue, path, settings));
                    }
                }

                foreach (var property in itemType.GetInstanceProperties())
                {
                    var propertyValue = property.GetValue(item);
                    if (propertyValue != null || !settings.IgnoreFieldsWithNullValue)
                    {
                        fieldsAndProperties.Add(ParseObject(property.Name, propertyValue, path, settings));
                    }
                }

                return(new ObjectNode(name, fieldsAndProperties));
            }
            finally
            {
                path.Remove(item);
            }
        }