inspecting an object's properties

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
you know how in the immediate window you can inspect an objects property, i
was just wondering if there was a way to display all that on my web page in a
div or something?

thanks,
rodchar
 
rodchar said:
you know how in the immediate window you can inspect an objects property,
i
was just wondering if there was a way to display all that on my web page
in a
div or something?

Not automatically, but you can program it by means of Reflection. For
instance, if you add to your web page a table with ID="tbl", you can display
the properties of an object with code that is similar to this:

using System.Reflection;
....
Type t = theObject.GetType();
PropertyInfo pia[] = t.GetProperties();
foreach (PropertyInfo pi in pia)
{
HtmlTableRow tr = new HtmlTableRow();
tbl.Rows.Add(tr);

HtmlTableCell td1 = new HtmlTableCell();
tr.Cells.Add(td1);
LiteralControl lc1 = new LiteralControl(pi.Name);
td1.Controls.Add(lc1);

object propertyValue = pi.GetValue(theObject, null);
HtmlTableCell td2 = new HtmlTableCell();
tr.Cells.Add(td2);
LiteralControl lc2 = new LiteralControl(propertyValue.ToString());
td2.Controls.Add(lc2);
}

This displays each of the properties converted to a string. If you wish to
recursively display the properties of each property, you will have to write
something more sophisticated, possibly using a tree control instead of a
table.
 
thanks for the help,
rod.

Alberto Poblacion said:
rodchar said:
you know how in the immediate window you can inspect an objects property,
i
was just wondering if there was a way to display all that on my web page
in a
div or something?

Not automatically, but you can program it by means of Reflection. For
instance, if you add to your web page a table with ID="tbl", you can display
the properties of an object with code that is similar to this:

using System.Reflection;
....
Type t = theObject.GetType();
PropertyInfo pia[] = t.GetProperties();
foreach (PropertyInfo pi in pia)
{
HtmlTableRow tr = new HtmlTableRow();
tbl.Rows.Add(tr);

HtmlTableCell td1 = new HtmlTableCell();
tr.Cells.Add(td1);
LiteralControl lc1 = new LiteralControl(pi.Name);
td1.Controls.Add(lc1);

object propertyValue = pi.GetValue(theObject, null);
HtmlTableCell td2 = new HtmlTableCell();
tr.Cells.Add(td2);
LiteralControl lc2 = new LiteralControl(propertyValue.ToString());
td2.Controls.Add(lc2);
}

This displays each of the properties converted to a string. If you wish to
recursively display the properties of each property, you will have to write
something more sophisticated, possibly using a tree control instead of a
table.
 
Back
Top