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, );
} 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 = ; i < level; i++) writer.Write(" ");
} private void WriteLine()
{
writer.WriteLine();
pos = ;
} private void WriteTab()
{
Write(" ");
while (pos % != ) 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("{ }");
}
}
}
C#.NET ObjectDumper的更多相关文章
- .NET 工具类ObjectDumper 打印对象
// Comes from the LINQ samples provided by Microsoft //Copyright (C) Microsoft Corporation. All righ ...
- RealProxy深入
Program.cs class Program { static void Main(string[] args) { NoMethodLogging(); Console.WriteLine(&q ...
- .NET (五)委托第五讲:内置委托Predicate
// 摘要: // 表示定义一组条件并确定指定对象是否符合这些条件的方法. // // 参数: // obj: // 要按照由此委托表示的方法中定义的条件进行比较的对象. // // 类型参数: // ...
- .NET (四)委托第四讲:内置委托Comparison
// 摘要: // 表示比较同一类型的两个对象的方法. // // 参数: // x: // 要比较的第一个对象. // // y: // 要比较的第二个对象. // // 类型参数: // T: / ...
- .NET (一)委托第一讲:什么是委托
1.为什么要使用委托? 生活中的委托就是委托他人帮我们去办一件事情,程序中的委托类似.看下面的例子 class Class1 { static void Main(String[] args) { L ...
- 白话LINQ系列2---以代码演进方式学习LINQ必备条件
今天,我们通过一个简单的示例代码的演进过程,来学习LINQ必备条件:隐式类型局部变量:对象集合初始化器:委托:匿名函数:lambda表达式:扩展方法:匿名类型.废话不多说,我们直接进入主题. 一.实现 ...
- 白话LINQ系列1---什么是LINQ?
一.本系列目标 1.理解LINQ: 2.能写得复杂的LINQ语句(比如:动态查询): 3.理解表达式树及相关概念: 4.熟练运用LINQ写出优美的代码(希望一起努力,最终达到): 二.LINQ为何物? ...
- 【EF学习笔记08】----------加载关联表的数据 显式加载
显式加载 讲解之前,先来看一下我们的数据库结构:班级表 学生表 加载从表集合类型 //显示加载 Console.WriteLine("=========查询集合===========&quo ...
- 【EF学习笔记06】----------加载关联表的数据 延迟加载
讲解之前,先来看一下我们的数据库结构:班级表 学生表 延迟加载 //延迟加载 using (var db = new Entities()) { //查询班级 var classes = (from ...
随机推荐
- WEB移动应用框架构想(转载)
iUI.jQTouch.WPTouch.PhoneGap.XUI.iWebkit.Rhodes.gwt-mobile…当我们已经开始惊 叹 web移动应用充斥着各种各样框架与类库的时候,其实各大web ...
- WebBrowser 禁用右键
禁用错误脚本提示 将 WebBrowser控件的 ScriptErrorsSuppressed 设为 true 禁用右键菜单 将 WebBrowser 的 IsWebBrowserContextMen ...
- 菜鸟写的第一个chrome插件
一.新建一个文件夹,用来放插件的代码 二.首先新建配置文件manifest.json // 开发参考:http://open.chrome.360.cn/extension_dev/overview. ...
- android-解决 Android N 上 报错:android.os.FileUriExposedException
解决 Android N 上 安装Apk时报错:android.os.FileUriExposedException: file:///storage/emulated/0/Download/appN ...
- 新浪微博客户端(17)-集成MJExtension
使用MJExtension框架将字典转换为模型 DJHomeViewController.m /** 载入新的微博数据 */ - (void)loadNewStatues { AFHTTPSessio ...
- [pdf.js]预览pdf时,中文名称乱码的问题
在项目中使用了pdf.js的方式预览pdf,但针对中文名称的时候会出现乱码,导致找不到该文件而出现错误. 解决办法 <script src="viewer.js" chars ...
- [整理]SSRS error:rsReportNotReady
直接访问http://XXX/ReportServer/Pages/ReportViewer.aspx?reportpath&rs%3aCommand=Render执行查询是没有问题的. 但是 ...
- 字符串模拟赛T2
// source code from laekov for c0x17 #define PRID "fkqh" #include <cstdio> #include ...
- [Effective JavaScript 笔记]第34条:在原型中存储方法
js中完全有可能不借助原型进行编程.不用在其原型中定义任何的方法. 创建对象 构造函数法 所有属性和方法都在构造函数中定义 function User(name,pwd){ this.name=nam ...
- [转]结合轮廓显示,实现完整的框选目标(附Demo代码)
原地址:http://www.cnblogs.com/88999660/articles/2887078.html 几次看见有人问框选物体的做法,之前斑竹也介绍过,用画的框生成的视椎,用经典图形学的视 ...