C#对象翻车机

我正在寻找一个类,可以输出一个对象和所有的叶值类似于这样的格式:

User - Name: Gordon - Age : 60 - WorkAddress - Street: 10 Downing Street - Town: London - Country: UK - HomeAddresses[0] ... - HomeAddresses[1] ... 

(或更清晰的格式)。 这将相当于:

 public class User { public string Name { get;set; } public int Age { get;set; } public Address WorkAddress { get;set; } public List<Address> HomeAddresses { get;set; } } public class Address { public string Street { get;set; } public string Town { get;set; } public string Country { get;set; } } 

PropertyGrid控件的一种string表示,减去必须为每个types实现大量devise器。

PHP有一些叫做var_dump的东西。 我不想用手表,因为这是打印出来的。

任何人都可以指向我这样的东西,如果它存在? 或者,写一个赏金。

sgmoore链接中的对象倾销者:

 //Copyright (C) Microsoft Corporation. All rights reserved. using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Reflection; // See the ReadMe.html for additional information 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("{ }"); } } } 

2015更新

YAML也很好地达到了这个目的,YamlDotNet就是这样做的

install-package YamlDotNet

  private static void DumpAsYaml(object o) { var stringBuilder = new StringBuilder(); var serializer = new Serializer(); serializer.Serialize(new IndentedTextWriter(new StringWriter(stringBuilder)), o); Console.WriteLine(stringBuilder); } 

您可以使用JSON序列化程序,这对于任何使用JSON的人来说都应该很容易阅读

 User theUser = new User(); theUser.Name = "Joe"; System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(myPerson.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, theUser ); string json = Encoding.Default.GetString(ms.ToArray()); 

您可以在CodePlex上findObjectDumper项目 。 您也可以通过Visual Studio 2010 Nuget包pipe理器添加它 。

如果你正在使用标记, System.Web.ObjectInfo.PrintASP.NET网页2 )将完成这个,很好地格式化为HTML。

例如:

 @ObjectInfo.Print(new { Foo = "Hello", Bar = "World", Qux = new { Number = 42, }, }) 

在一个网页,产生:

ObjectInfo.Print(...)

这是我写的一个视觉工作室扩展:

https://visualstudiogallery.msdn.microsoft.com/c6a21c68-f815-4895-999f-cd0885d8774f

在行动: 对象导出器在行动

你可以很容易地写一点反思。 有点像:

 public void Print(object value, int depth) { foreach(var property in value.GetType().GetProperties()) { var subValue = property.GetValue(value); if(subValue is IEnumerable) { PrintArray(property, (IEnumerable)subValue); } else { PrintProperty(property, subValue); } } } 

你可以写出PrintArray和PrintProperty方法。

我有一个方便的T.Dump()扩展方法 ,应该非常接近你正在寻找的结果。 作为一种扩展方法,它是非侵入性的,应该在所有POCO对象上工作。

用法示例

 var model = new TestModel(); Console.WriteLine(model.Dump()); 

示例输出

 { Int: 1, String: One, DateTime: 2010-04-11, Guid: c050437f6fcd46be9b2d0806a0860b3e, EmptyIntList: [], IntList: [ 1, 2, 3 ], StringList: [ one, two, three ], StringIntMap: { a: 1, b: 2, c: 3 } } 

我知道这是一个古老的问题,但我想我会抛出一个替代scheme为我工作,花了我大约两分钟的时间做。

安装Newtonsoft Json.NET:http://james.newtonking.com/json

(或nuget版本) http://www.nuget.org/packages/newtonsoft.json/

参考汇编:

 using Newtonsoft.Json; 

转储JSONstring以logging:

 txtResult.Text = JsonConvert.SerializeObject(testObj); 

如果您不想复制和粘贴Chris S的代码,Visual Studio 2008示例将附带一个ObjectDumper。

Drive:\ Program Files \ Microsoft Visual Studio 9.0 \ Samples \ 1033 \ LinqSamples \ ObjectDumper

这是一个替代scheme:

 using System.Reflection; public void Print(object value) { PropertyInfo[] myPropertyInfo; string temp="Properties of "+value+" are:\n"; myPropertyInfo = value.GetType().GetProperties(); for (int i = 0; i < myPropertyInfo.Length; i++) { temp+=myPropertyInfo[i].ToString().PadRight(50)+" = "+myPropertyInfo[i].GetValue(value, null)+"\n"; } MessageBox.Show(temp); } 

(只是触及1级,没有深度,但说了很多)

对于大多数类,您可以使用DataContractSerializer