Simple object dumping extension method for CSharp


Here’s a useful little extension method I created to dump fields and properties to the console of an object in CSharp.  

It demonstrates simple reflection and extension method syntax.



public static class ObjectExtensions
public static void DumpProperties(this object obj)
foreach (var fld in obj.GetType().GetFields())
Console.WriteLine(String.Format("{0} = \"{1}\"", fld.Name, fld.GetValue(obj)));
foreach (var prop in obj.GetType().GetProperties())
if(prop.CanRead) Console.WriteLine(String.Format("{0} = \"{1}\"", prop.Name,prop.GetValue(obj, null)));




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s