Example #1
0
        private string TryReturnFinalRecursion(object obj, out Type type, string fullName, int nestingLevel)
        {
            type = null;

            if (obj == null)
            {
                return("null");
            }

            type = obj.GetType();

            if (serializator.TrySerializate(obj, type, fullName, out var result))
            {
                return(result);
            }

            if (FinalTypes.IsFinalType(type))
            {
                return(obj.ToString());
            }

            objects.Add(obj);

            if (obj is ICollection collection)
            {
                return(PrintToStringCollection(collection, nestingLevel));
            }

            return(null);
        }
Example #2
0
        private string PrintToString(object obj, int nestingLevel, string fullName = "", bool fromCollection = false)
        {
            var result = TryReturnFinalRecursion(obj, out var type, fullName, nestingLevel);

            if (result != null)
            {
                return(result + (!fromCollection ? Environment.NewLine : ""));
            }

            var identation = new string('\t', nestingLevel + 1);

            var sb = new StringBuilder();

            sb.AppendLine(type.Name);
            foreach (var propertyInfo in type.GetProperties())
            {
                if (CanConsiderProperty())
                {
                    sb.Append(identation + propertyInfo.Name + " = " +
                              PrintToString(propertyInfo.GetValue(obj),
                                            nestingLevel + 1, GetFullName()));
                }

                string GetFullName() => fullName == null ? null : fullName + '.' + propertyInfo.Name;

                bool CanConsiderProperty() => !excluder.IsExclude(propertyInfo.PropertyType, GetFullName()) &&
                (FinalTypes.IsFinalType(propertyInfo.PropertyType) || !IsReferenceCircle(propertyInfo.GetValue(obj)));
            }
            return(sb.ToString());

            bool IsReferenceCircle(object obj) => objects.Any(o => ReferenceEquals(o, obj));
        }