Subscribe | Alerts via Email
View All Quotes
“Nearly all men can stand adversity, but if you want to test a man's character, give him power.”
-Abraham Lincoln
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

©2010 Cal Zant
Sign In
Total Posts: 106
This Year: 5
This Month: 1
This Week: 0
Comments: 2

Here is a code snippet that uses a little reflection to return a string representing all of the various fields and properties contained in the given object, and their related values.  Because it uses reflection it has a higher cost at runtime than typical strongly-typed code ... but there are still some scenarios this type of discovery is useful.

public static string GetObjectDump(Object thisObject)
{
    StringBuilder sb = new StringBuilder();
    Type thisType = thisObject.GetType();

    var fields = thisType.GetFields();
    foreach (var f in fields)
        sb.Append(String.Format("{0} = {1}", f.Name, f.GetValue(thisObject)) + Environment.NewLine);

    var properties = thisType.GetProperties();
    foreach (var p in properties)
        sb.Append(String.Format("{0} = {1}", p.Name, p.GetValue(thisObject, null)) + Environment.NewLine);

    return sb.ToString();
}
Tuesday, October 14, 2008 11:18:48 AM (Central Standard Time, UTC-06:00)  #