.NET 工具類ObjectDumper 列印對象

來源:互聯網
上載者:User

標籤:str   field   對象   sys   reflect   string   ros   property   member   

// Comes from the LINQ samples provided by Microsoft//Copyright (C) Microsoft Corporation.  All rights reserved.using System;using System.IO;using System.Collections;using System.Collections.Generic;using System.Reflection;public class ObjectDumper {    public static void Write(object element)    {        Write(element, 0);    }    public static void Write(object element, int depth)    {        Write(element, depth, Console.Out);    }    public static void Write(object element, int depth, TextWriter log)    {        ObjectDumper dumper = new ObjectDumper(depth);        dumper.writer = log;        dumper.WriteObject(null, element);    }    TextWriter writer;    int pos;    int level;    int depth;    private ObjectDumper(int depth)    {        this.depth = depth;    }    private void Write(string s)    {        if (s != null) {            writer.Write(s);            pos += s.Length;        }    }    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 % 8 != 0) Write(" ");    }    private void WriteObject(string prefix, object element)    {        if (element == null || element is ValueType || element is string) {            WriteIndent();            Write(prefix);            WriteValue(element);            WriteLine();        }        else {            IEnumerable enumerableElement = element as IEnumerable;            if (enumerableElement != null) {                foreach (object item in enumerableElement) {                    if (item is IEnumerable && !(item is string)) {                        WriteIndent();                        Write(prefix);                        Write("...");                        WriteLine();                        if (level < depth) {                            level++;                            WriteObject(prefix, item);                            level--;                        }                    }                    else {                        WriteObject(prefix, item);                    }                }            }            else {                MemberInfo[] members = element.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);                        Write("=");                        Type t = f != null ? f.FieldType : p.PropertyType;                        if (t.IsValueType || t == typeof(string)) {                            WriteValue(f != null ? f.GetValue(element) : p.GetValue(element, null));                        }                        else {                            if (typeof(IEnumerable).IsAssignableFrom(t)) {                                Write("...");                            }                            else {                                Write("{ }");                            }                        }                    }                }                if (propWritten) WriteLine();                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(element) : p.GetValue(element, 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());        }        else if (o is ValueType || o is string) {            Write(o.ToString());        }        else if (o is IEnumerable) {            Write("...");        }        else {            Write("{ }");        }    }}

此類可以列印任意 對象 集合 數組。

使用

List<int> list = new List<int>();ObjectDumper.Write(list);

 

.NET 工具類ObjectDumper 列印對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.