.NET 工具类ObjectDumper 打印对象
// 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, );
} 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("{ }");
}
}
}
此类可以打印任意 对象 集合 数组。
使用
List<int> list = new List<int>(); ObjectDumper.Write(list);
.NET 工具类ObjectDumper 打印对象的更多相关文章
- java工具类之按对象中某属性排序
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...
- 黑马程序员——【Java基础】——泛型、Utilities工具类、其他对象API
---------- android培训.java培训.期待与您交流! ---------- 一.泛型 (一)泛型概述 1.泛型:JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制 ...
- Java工具类 通过ResultSet对象返回对应的实体List集合
自从学了JDBC用多了像一下这种代码: List<xxx> list = new Array<xxx>(); if(rs.next()){ xxx x = new xxx(); ...
- Java多线程并发工具类-信号量Semaphore对象讲解
Java多线程并发工具类-Semaphore对象讲解 通过前面的学习,我们已经知道了Java多线程并发场景中使用比较多的两个工具类:做加法的CycliBarrier对象以及做减法的CountDownL ...
- Java 常用工具类之基本对象包装类
为了方便操作基本数据类型值, 将其包装成对象, 在对象中定义了属性和行为, 丰富了该数据的操作. 用于描述该对象的类就称为基本数据类型对象包装类. 基本数据类型对应关系 基本数据类型(8种) 包装类 ...
- Java之工具类:判断对象是否为空或null
import java.lang.reflect.Array; import java.util.Collection; import java.util.Map; /** * 判断对象是否为空或nu ...
- 对象工具类 - ObjectUtils.java
对象工具类,提供对象克隆.获取对象属性.类型判断.Map转换对象.对象转Map.设置对象属性等. 源码如下:(点击下载 - ObjectUtils.java .JsonUtils.java .gso ...
- Spring普通类/工具类获取并调用Spring service对象的方法
参考<Spring普通类获取并调用Spring service方法>,网址:https://blog.csdn.net/jiayi_0803/article/details/6892455 ...
- BeanUtils 工具类
一.BeanUtils 概述 BeanUtils 是阿帕奇提供的一套专门用于将一些数据封装到java对象中的工具类; 名词:javaBean:特定格式的java类称为java ...
随机推荐
- php区分new static 和new self
关键点在于一个是静态绑定,一个是延迟绑定 <?php class A{ public function __construct() { } public function createObjSt ...
- inotify +rsync进行实时同步
1.安装rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpmyum -y install ino ...
- cuda-convnet windows8下编译
编译环境: windows8.1 Anaconda python2.7 Visual studio 2012 CUDA6.0 Pthread for windows Intel Math Kernel ...
- linux 1-100的累加
[ ] 判断式.它的使用和test命令一样 [ ]的判断符,只会返回2种值.0(真) 非0(假) -gt 大于-lt 小于-eq 等于-ne 不等于-ge 大于等于-le 小于等于 while ...
- linux的零碎使用
一.Linux(rehat.centos.ubuntu...)基础知识 上午: putty软件连接linux服务器: [root @ foundation2 ~ ] # 用户名 ...
- Android framework完整源码下载
包括cpp等native代码.可zip打包下载. https://github.com/android/platform_frameworks_base/branches/stale Android线 ...
- stdcall, cdecl, pascal 区别(转载)
转载自:http://www.cnblogs.com/lidabo/archive/2012/11/21/2781484.html stdcall, cdecl, pascal 区别 这三个参数都是告 ...
- 利用nodejs搭建服务器,测试AJAX
最近学习了AJAX一直没有进行过测试,前今天了解了Noejs搭建本地服务器下就尝试了一下.通过AJAX请求的方式获取HTTP服务器返回数据的代码 首先创建一个serve.js的文件.并写入以下代码. ...
- easyUI datagrid中 checkbox 各属性和事件
DataGrid其中与选择,勾选相关 DataGrid属性:singleSelect boolean 如果为true,则只允许选择一行. false ctrlSelect boolean 在启用多行 ...
- No Architectures to Compile for (ONLY_ACTIVE_ARCH=
No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=armv7, VA 运行报错 出现的原因:armv7s是应用在iP ...