Just another tech site

Print anything using reflection


At some point we all debugged an application using the “printf” strategy. I agree, it is not the most efficient approach but it can help in some cases. So here is a small bit of code that uses reflection to print the details of an object.
Again this is very simple and be extended

public static void PrintDetail(object o)
        {
            Type t = o.GetType();
            Console.WriteLine("[{0}]", t.FullName);
            PropertyInfo[] props = t.GetProperties();
            foreach (PropertyInfo prp in props)
            {
                object value = prp.GetValue(o, new object[] { });
                Console.Write("\t {0}: {1}", prp.Name, value);
            }
            Console.WriteLine("");
        }

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Tag Cloud

%d bloggers like this: