首页 / C# / C#.NET ObjectDumper
C#.NET ObjectDumper
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#.NET ObjectDumper,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3722字,纯文字阅读大概需要6分钟。
内容图文
demo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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); } publicstaticvoid Write(object element, int depth) { Write(element, depth, Console.Out); } publicstaticvoid 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; } privatevoid Write(string s) { if (s != null) { writer.Write(s); pos += s.Length; } } privatevoid WriteIndent() { for (int i = 0; i < level; i++) writer.Write(""); } privatevoid WriteLine() { writer.WriteLine(); pos = 0; } privatevoid WriteTab() { Write(""); while (pos % 8 != 0) Write(""); } privatevoid WriteObject(string prefix, object element) { if (element == null || element is ValueType || element isstring) { 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 isstring)) { 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--; } } } } } } } } privatevoid WriteValue(object o) { if (o == null) { Write("null"); } elseif (o is DateTime) { Write(((DateTime)o).ToShortDateString()); } elseif (o is ValueType || o isstring) { Write(o.ToString()); } elseif (o is IEnumerable) { Write("..."); } else { Write("{ }"); } } }
原文:http://www.cnblogs.com/songsz1/p/3553845.html
内容总结
以上是互联网集市为您收集整理的C#.NET ObjectDumper全部内容,希望文章能够帮你解决C#.NET ObjectDumper所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。