Object Dumper : Debug Trace « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Development Class » Debug TraceScreenshots 
Object Dumper
        
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Linq;

namespace Lucene.Linq.Utility
{
    public class ObjectDumper {
        const ConsoleColor CIdent = ConsoleColor.Cyan;
        const ConsoleColor CValue = ConsoleColor.Magenta;

        public static void Write(object o) {
            Write(o, 0);
        }

        public static void Write(object o, int depth) {
            ObjectDumper dumper = new ObjectDumper(depth);
            dumper.WriteObject(null, o);
        }

        TextWriter _writer;
        int _pos;
        int _level;
        int _depth;

        private ObjectDumper(int depth) {
            this._writer = Console.Out;
            this._depth = depth;
        }

        private void Write(string s) {
            if (s != null) {
                _writer.Write(s);
                _pos += s.Length;
            }
        }

        private void Write(string s, ConsoleColor c) {
            ConsoleColor temp = Console.ForegroundColor;
            Console.ForegroundColor = c;
            Write(s);
            Console.ForegroundColor = temp;
        }

        private void WriteIndent() {
            for (int i = 0; i < _level; i++_writer.Write("  ");
        }

        private void WriteLine() {
            _writer.WriteLine();
            _pos = 0;
        }

        private void WriteTab() {
            Write("  ");
            while (_pos % != 0Write(" ");
        }

        private void WriteObject(string prefix, object o) {
            if (o == null || o is ValueType || o is string || o is XElement) {
                WriteIndent();
                Write(prefix);
                WriteValue(o);
                WriteLine();
            else if (o is IEnumerable) {
                foreach (object element in (IEnumerable)o) {
                    if (element is IEnumerable && !(element is string)) {
                        WriteIndent();
                        Write(prefix);
                        Write("...");
                        WriteLine();
                        if (_level < _depth) {
                            _level++;
                            WriteObject(prefix, element);
                            _level--;
                        }
                    else {
                        WriteObject(prefix, element);
                    }
                }
            else {
                MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance);
                WriteIndent();
                Write(prefix);
                bool propWritten = false;
                foreach (MemberInfo m in members) {
                    FieldInfo f = m as FieldInfo;
                    PropertyInfo p = m as PropertyInfo;
                    if (f != null || p != null) {
                        if (propWritten) {
                            WriteTab();
                        else {
                            propWritten = true;
                        }
                        Write(m.Name, CIdent);
                        Write("=");
                        Type t = f != null ? f.FieldType : p.PropertyType;
                        if (t.IsValueType || t == typeof(string)) {
                            WriteValue(f != null ? f.GetValue(o: p.GetValue(o, null));
                        else {
                            if (typeof(IEnumerable).IsAssignableFrom(t)) {
                                Write("...");
                            else {
                                Write("{ }");
                            }
                        }
                    }
                }
                if (propWrittenWriteLine();
                if (_level < _depth) {
                    foreach (MemberInfo m in members) {
                        FieldInfo f = m as FieldInfo;
                        PropertyInfo p = m as PropertyInfo;
                        if (f != null || p != null) {
                            Type t = f != null ? f.FieldType : p.PropertyType;
                            if (!(t.IsValueType || t == typeof(string))) {
                                object value = f != null ? f.GetValue(o: p.GetValue(o, null);
                                if (value != null) {
                                    _level++;
                                    WriteObject(m.Name + ": ", value);
                                    _level--;
                                }
                            }
                        }
                    }
                }
            }
        }

        private void WriteValue(object o) {
            if (o == null) {
                Write("null");
            else if (o is DateTime) {
                Write(((DateTime)o).ToShortDateString(), CValue);
            else if (o is ValueType || o is string || o is XElement) {
                Write(o.ToString(), CValue);
            else if (o is IEnumerable) {
                Write("...");
            else {
                Write("{ }");
            }
        }
    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.Using BooleanSwitchUsing BooleanSwitch
2.Debug class
3.Debug and ProfileDebug and Profile
4.Trace to event log
5.Trace to debuger: writeline and flush
6.Trace class: listener and writeline
7.Tracing To A File
8.Tracing Example
9.demonstrates debug outputdemonstrates debug output
10.illustrate the use of the debuggerillustrate the use of the debugger
11.A simple demonstration of the Debug class
12.Demonstrate indenting debug messages
13.Demonstrates routing debug messages to a fileDemonstrates routing debug messages to a file
14.Defensive Programming:Conditional Methods
15.Debug and Trace Output
16.Using Switches to Control Debug and Trace:BooleanSwitch
17.Using Switches to Control Debug and Trace:TraceSwitch
18.Using Switches to Control Debug and Trace:User-Defined Switch
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.