I needed a quick’n’dirty way to inspect objects returned from external source. I wrote simple object properties dumping mechanism you can use to investigate unknown objects. It is really quick and really dirty.
using System.Collections.Generic; using System.IO;
privatestaticDictionary<string, string> GetProperties(object obj) { var props = newDictionary<string, string>(); if (obj == null) return props;
var type = obj.GetType(); foreach (var prop in type.GetProperties()) { var val = prop.GetValue(obj, newobject[] { }); var valStr = val == null ? "" : val.ToString(); props.Add(prop.Name, valStr); }
return props; } } }
If you are using console application you can create properties dump using the following code.
ObjectDump.Write(Console.Out,objFromSrv);
The result is something like this.
And we are done. This code is not intended to use in live environments. Use it when you are investigating different mysteries created by someone else. :)
Liked this post? Empower your friends by sharing it!
Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.