Example #1
0
            public void DumpObject(object?o, bool avoidDumpEntity = false)
            {
                if (o == null)
                {
                    Sb.Append("null");
                    return;
                }

                if (o is Type type)
                {
                    Sb.Append("typeof(");
                    Sb.Append(CSharpRenderer.TypeName(type));
                    Sb.Append(")");
                    return;
                }

                Type t = o.GetType();

                if (IsDelegate(t))
                {
                    Sb.Append("[DELEGATE]");
                    return;
                }

                if (IsBasicType(t) || t.IsValueType)
                {
                    Sb.Append(DumpValue(o !));
                    return;
                }

                Sb.Append("new ");

                Sb.Append(CSharpRenderer.CleanIdentifiers(CSharpRenderer.TypeName(t)));

                if (IgnoreTypes.Contains(t))
                {
                    Sb.Append("{ " + o !.ToString() + " }");
                    return;
                }

                if (objects.Contains(o !))
                {
                    if (o is Entity ent)
                    {
                        Sb.Append("({0}{1})".FormatWith(
                                      ent.IsNew ? "IsNew": ent.IdOrNull.ToString(),
                                      ent == null ? null : ", ticks: " + ent.ticks
                                      ));
                    }
                    if (o is Lite <Entity> )
                    {
                        var id = ((Lite <Entity>)o).IdOrNull;
                        Sb.Append(id.HasValue ? "({0})".FormatWith(id.Value) : "");
                    }
                    Sb.Append(" /* [ALREADY] {0} */".FormatWith(SafeToString(o !)));
                    return;
                }

                objects.Add(o !);

                if (o is Entity e)
                {
                    Sb.Append("({0}{1})".FormatWith(
                                  e.IsNew ? "IsNew" : e.IdOrNull.ToString(),
                                  e.ticks == 0 ? null : ", ticks: " + e.ticks
                                  ));

                    string toString = SafeToString(o);

                    Sb.Append(" /* {0} {1} */".FormatWith(toString, (avoidDumpEntity ? "[DUMP AS LITE]" : "")));

                    if (avoidDumpEntity)
                    {
                        return;
                    }
                }

                if (o is Lite <Entity> l)
                {
                    Sb.Append("({0}, \"{1}\")".FormatWith((l.IdOrNull.HasValue ? l.Id.ToString() : "null"), l.ToString()));
                    if (((Lite <Entity>)o).EntityOrNull != null && !avoidDumpEntity)
                    {
                        Sb.AppendLine().AppendLine("{".Indent(level));
                        level += 1;
                        var prop = o.GetType().GetProperty(nameof(Lite <Entity> .Entity)) !;
                        DumpPropertyOrField(prop.PropertyType, prop.Name, prop.GetValue(o, null) !);
                        level -= 1;
                        Sb.Append("}".Indent(level));
                    }
                    return;
                }

                if (o is IEnumerable ie && !Any(ie))
                {
                    Sb.Append("{}");
                    return;
                }

                if (o is byte[] && !showByteArrays)
                {
                    Sb.Append("{...}");
                    return;
                }

                Sb.AppendLine().AppendLine("{".Indent(level));
                level += 1;

                if (t.Namespace.HasText() && t.Namespace.StartsWith("System.Reflection"))
                {
                    Sb.AppendLine("ToString = {0},".FormatWith(SafeToString(o !)).Indent(level));
                }
                else if (o is Exception ex)
                {
                    DumpPropertyOrField(typeof(string), "Message", ex.Message);
                    DumpPropertyOrField(typeof(string), "StackTrace", ex.StackTrace);
                    DumpPropertyOrField(typeof(Exception), "InnerException", ex.InnerException);
                    DumpPropertyOrField(typeof(IDictionary), "Data", ex.Data);
                }
                else if (o is IEnumerable)
                {
                    if (o is IDictionary dic)
                    {
                        foreach (DictionaryEntry?item in dic)
                        {
                            Sb.Append("{".Indent(level));
                            DumpObject(item !.Value.Key);
                            Sb.Append(", ");
                            DumpObject(item !.Value.Value);
                            Sb.AppendLine("},");
                        }
                    }
                    else
                    {
                        foreach (var item in (o as IEnumerable) !)
                        {
                            Sb.Append("".Indent(level));
                            DumpObject(item, avoidDumpEntity);
                            Sb.AppendLine(",");
                        }
                    }
                }
                else if (!typeof(ModifiableEntity).IsAssignableFrom(t))
                {
                    foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {
                        var hasAvoidDumpEntityAttr = prop.HasAttribute <AvoidDumpEntityAttribute>();
                        DumpPropertyOrField(prop.PropertyType, prop.Name, prop.GetValue(o, null), hasAvoidDumpEntityAttr);
                    }
                }
                else
                {
                    foreach (var field in Reflector.InstanceFieldsInOrder(t).OrderBy(IsMixinField))
                    {
                        if (IsIdOrTicks(field))
                        {
                            continue;
                        }

                        var hasAvoidDumpEntityAttr = field.HasAttribute <AvoidDumpEntityAttribute>() || Reflector.TryFindPropertyInfo(field)?.HasAttribute <AvoidDumpEntityAttribute>() == true;

                        if (IsMixinField(field))
                        {
                            var val = field.GetValue(o);

                            if (val == null)
                            {
                                continue;
                            }

                            DumpPropertyOrField(field.FieldType, GetFieldName(field), val, hasAvoidDumpEntityAttr);
                        }

                        var skip = this.showIgnoredFields == ShowIgnoredFields.Yes ? false :
                                   this.showIgnoredFields == ShowIgnoredFields.OnlyQueryables ? IsIgnored(field) && Reflector.TryFindPropertyInfo(field)?.HasAttribute <QueryablePropertyAttribute>() != true :
                                   this.showIgnoredFields == ShowIgnoredFields.No ? IsIgnored(field) :
                                   throw new InvalidOperationException("Unexpected ShowIgnoredFields");

                        if (!skip)
                        {
                            DumpPropertyOrField(field.FieldType, GetFieldName(field), field.GetValue(o), hasAvoidDumpEntityAttr);
                        }
                    }
                }

                level -= 1;
                Sb.Append("}".Indent(level));
                return;
            }
            public void DumpObject(object o)
            {
                if (o == null)
                {
                    Sb.Append("null");
                    return;
                }

                if (o is Type)
                {
                    Sb.Append("typeof(");
                    Sb.Append(CSharpRenderer.TypeName((Type)o));
                    Sb.Append(")");
                    return;
                }

                Type t = o.GetType();

                if (IsDelegate(t))
                {
                    Sb.Append("[DELEGATE]");
                    return;
                }

                if (IsBasicType(t) || t.IsValueType)
                {
                    Sb.Append(DumpValue(o));
                    return;
                }

                Sb.Append("new ");

                Sb.Append(CSharpRenderer.CleanIdentifiers(CSharpRenderer.TypeName(t)));

                if (IgnoreTypes.Contains(t))
                {
                    Sb.Append("{ " + o.ToString() + " }");
                    return;
                }

                if (objects.Contains(o))
                {
                    if (o is Entity)
                    {
                        var ident = o as Entity;
                        var ent   = o as Entity;

                        Sb.Append("({0}{1})".FormatWith(
                                      ident.IsNew ? "IsNew": ident.IdOrNull.ToString(),
                                      ent == null ? null : ", ticks: " + ent.ticks
                                      ));
                    }
                    if (o is Lite <Entity> )
                    {
                        var id = ((Lite <Entity>)o).IdOrNull;
                        Sb.Append(id.HasValue ? "({0})".FormatWith(id.Value) : "");
                    }
                    Sb.Append(" /* [CICLE] {0} */".FormatWith(SafeToString(o)));
                    return;
                }

                objects.Add(o);

                if (o is Entity)
                {
                    var ent = (Entity)o;
                    Sb.Append("({0}{1})".FormatWith(
                                  ent.IsNew ? "IsNew" : ent.IdOrNull.ToString(),
                                  ent.ticks == 0 ? null : ", ticks: " + ent.ticks
                                  ));

                    string toString = SafeToString(o);

                    Sb.Append(" /* {0} */".FormatWith(toString));
                }

                if (o is Lite <Entity> )
                {
                    var l = o as Lite <Entity>;
                    Sb.Append("({0}, \"{1}\")".FormatWith((l.IdOrNull.HasValue ? l.Id.ToString() : "null"), l.ToString()));
                    if (((Lite <Entity>)o).UntypedEntityOrNull == null)
                    {
                        return;
                    }
                }

                if (o is IEnumerable && !Any((o as IEnumerable)))
                {
                    Sb.Append("{}");
                    return;
                }

                if (o is byte[] && !showByteArrays)
                {
                    Sb.Append("{...}");
                    return;
                }

                Sb.AppendLine().AppendLine("{".Indent(level));
                level += 1;

                if (t.Namespace.HasText() && t.Namespace.StartsWith("System.Reflection"))
                {
                    Sb.AppendLine("ToString = {0},".FormatWith(SafeToString(o)).Indent(level));
                }
                else if (o is Exception)
                {
                    var ex = o as Exception;
                    DumpPropertyOrField(typeof(string), "Message", ex.Message);
                    DumpPropertyOrField(typeof(string), "StackTrace", ex.StackTrace);
                    DumpPropertyOrField(typeof(Exception), "InnerException", ex.InnerException);
                    DumpPropertyOrField(typeof(IDictionary), "Data", ex.Data);
                }
                else if (o is IEnumerable)
                {
                    if (o is IDictionary)
                    {
                        foreach (DictionaryEntry item in (o as IDictionary))
                        {
                            Sb.Append("{".Indent(level));
                            DumpObject(item.Key);
                            Sb.Append(", ");
                            DumpObject(item.Value);
                            Sb.AppendLine("},");
                        }
                    }
                    else
                    {
                        foreach (var item in (o as IEnumerable))
                        {
                            Sb.Append("".Indent(level));
                            DumpObject(item);
                            Sb.AppendLine(",");
                        }
                    }
                }
                else if (!typeof(ModifiableEntity).IsAssignableFrom(t))
                {
                    foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                    {
                        DumpPropertyOrField(prop.PropertyType, prop.Name, prop.GetValue(o, null));
                    }
                }
                else
                {
                    foreach (var field in Reflector.InstanceFieldsInOrder(t).OrderBy(IsMixinField))
                    {
                        if (IsIdOrTicks(field))
                        {
                            continue;
                        }

                        if (IsMixinField(field))
                        {
                            var val = field.GetValue(o);

                            if (val == null)
                            {
                                continue;
                            }

                            DumpPropertyOrField(field.FieldType, GetFieldName(field), val);
                        }

                        if (!showIgnoredFields && (field.HasAttribute <IgnoreAttribute>()) || (Reflector.TryFindPropertyInfo(field)?.HasAttribute <IgnoreAttribute>() == true))
                        {
                            continue;
                        }

                        DumpPropertyOrField(field.FieldType, GetFieldName(field), field.GetValue(o));
                    }
                }

                level -= 1;
                Sb.Append("}".Indent(level));
                return;
            }