Jwalin Khatri

Display Object Properties value in one string using System.Reflaction

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());

Leave a Reply

Blog Stats

  • 16,458 hits

  • Kingsley Tagbo: Thanks for reading and sharing my article. Have a great like ... Kingsley
  • Bob: Doesn't work with IIS7 (default config on Windows 7 and 2008) because no ADSI anymore.
  • jwalin: Follow the following steps 1]. open you Page in NOTEPAD. 2]. find the tag 3]. Replace the "body" like body oncontextmenu=”return false;" 4]. Sav