Posted by: jwalin on: December 16, 2008
override the ToString() method to make a nice ledgiable version of my object.
1 public override string ToString() 2 { 3 StringBuilder sb = new StringBuilder(); 4 5 Type type = this.GetType(); 6 PropertyInfo[] props = type.GetProperties(); 7 8 sb.AppendLine("======= START OBJECT ======="); 9 foreach (PropertyInfo pi in props) 10 { 11 sb.AppendFormat("{0}: {1}\n", pi.Name, pi.GetValue(this,null)); 12 } 13 sb.AppendLine("======= END OBJECT ======="); 14 15 return sb.ToString(); 16 }
This is using System.Reflection. To use:
Trace.Write("OutputBusinessObject", myObject.ToString());