zoukankan      html  css  js  c++  java
  • C#.NET ObjectDumper

    demo:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    
    namespace MYLinqConsole
    {
        public class ProcessGet
        {
            public static void DisplayProcesses()
            {
                List<processData> processes = new List<processData>();
                processData data;
                foreach (var item in Process.GetProcesses())
                {
                    data = new processData();
                    data.ID = item.Id;
                    data.Name = item.ProcessName;
                    data.Memory = item.WorkingSet64;
                    if (data != null) processes.Add(data);
                }
    
                ObjectDumper.Write(processes);
            }
    
            class processData
            {
                public Int32 ID { get; set; }
                public Int64 Memory { get; set; }
                public string Name { get; set; }
            }
        }
    
    
    }
    

      

    ObjectDumper类:

    //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("{ }");
    }
    }
    }
  • 相关阅读:
    EntityFramework之创建数据库及基本操作(一)
    Entity Framework Code First (八)迁移 Migrations
    [转]Tomcat启动报错:AnnotationConfigBeanDefinitionParser are only available on JDK 1.5 and higher
    [转]bootstrap table本地数据使用方法
    [转]MySQL-5.7 Update语句详解
    [转]操作MySQL数据库报出:Parameter index out of range (1 > number of parameters, which is
    [转]在MySQL中创建实现自增的序列(Sequence)的教程
    [转]MySQL如何设置自动增长序列 SEQUENCE
    [转]Mustache 使用心得总结
    [转]关于重定向RedirectAttributes的用法
  • 原文地址:https://www.cnblogs.com/songsz1/p/3553845.html
Copyright © 2011-2022 走看看