Wednesday, August 13, 2008

Use reflection to dump properies

 For debugging purposes, we can use the following reflection based code:

public static string DumpOrderBean(OrderBean ob)
{

PropertyInfo[] pi = typeof(OrderBean).GetProperties();
StringBuilder sb= new StringBuilder();
foreach (PropertyInfo p in pi)
{
if (p.Name.IndexOf("Bean")<0)
sb.Append(p.Name +" :"+ p.GetValue(ob, null)+"\r\n");
}
StreamWriter w= new StreamWriter(@"c:\JGBRollTestOB.txt");
w.Write(sb.ToString()); w.Flush();

OrderAllocationBean oa = ob.allocationBeans[0];
PropertyInfo[] pi2 = typeof(OrderAllocationBean).GetProperties();
StringBuilder sb2 = new StringBuilder();
foreach (PropertyInfo p2 in pi2)
{
if (p2.Name.IndexOf("Bean") < 0)
sb2.Append(p2.Name + ":" + p2.GetValue(oa, null) + "\r\n");
}
StreamWriter w2 = new StreamWriter(@"c:\JGBRollTestOAB.txt");
w2.Write(sb2.ToString()); w2.Flush();
return sb.ToString();
}


No comments: