T
Torre Quinn
I was just performing the following inorder to gain a deeper understanding
of reflection and exception handling, so don't anyone go breaking their
backs to help me. That said I cant figure out how to accomplish something. I
was trying to write a function that would except an object (in this case an
exception) and a Tree View control and then populate the tree view with the
properties and values of the properties of the object.
The code below works to a point. It does iterate the properties and build
the tree view with the values as I would like it to however it works only to
the first "teir", for lack of a bettter word, of the properties in the
object.
For example, if I pass in an exception the tree view will populate with the
Message, InnerException, CallStack, etc. It will not, however, create the
nodes for the InnerException. I can't figure out how to get to the nested
properties.
(by the way to make the below code run remark out the foreach block below
//iterate the sub properties)
If anyone could be so kind as to point me in the right direction I would
appreciate it.
Thanks
public void PopulateTreeViewFromObject(TreeView ctrl, object
obj, bool clearControl)
{
TreeNode node;
//clear the tree view control if asked to do so
if(clearControl)
{
ctrl.Nodes.Clear();
}
// Display information for all properties.
foreach(PropertyInfo pi in obj.GetType().GetProperties())
{
node=new TreeNode();
//get the properties of the exception object
PropertyInfo propInfo = (PropertyInfo)pi;
//collect the property info for this node
PopulateTreeViewFromExceptionPropertyInfo(propInfo,node,obj);
//add the node to the control
ctrl.Nodes.Add(node);
}
}
public static void
PopulateTreeViewFromExceptionPropertyInfo(PropertyInfo prop, TreeNode node,
object obj)
{
//collect the property info for this node
node.Text = prop.Name.ToString();
PropertyInfo currentProp =
(obj.GetType().GetProperty(node.Text.ToString()));
if (currentProp.GetValue(obj,null) != null)
{
TreeNode nodeValue = new TreeNode();
nodeValue.Text=currentProp.GetValue(obj,null).ToString();
node.Nodes.Add(nodeValue);
}
//iterate the sub properties
foreach(PropertyInfo pi in
currentProp.GetType().GetProperties())
{
TreeNode subNode=new TreeNode();
//get the properties of the exception object
PropertyInfo subPropInfo = (PropertyInfo)pi;
//collect the property info for this node
PopulateTreeViewFromExceptionPropertyInfo(subPropInfo,subNode,obj);
//add the node to the control
node.Nodes.Add(subNode);
}
}
of reflection and exception handling, so don't anyone go breaking their
backs to help me. That said I cant figure out how to accomplish something. I
was trying to write a function that would except an object (in this case an
exception) and a Tree View control and then populate the tree view with the
properties and values of the properties of the object.
The code below works to a point. It does iterate the properties and build
the tree view with the values as I would like it to however it works only to
the first "teir", for lack of a bettter word, of the properties in the
object.
For example, if I pass in an exception the tree view will populate with the
Message, InnerException, CallStack, etc. It will not, however, create the
nodes for the InnerException. I can't figure out how to get to the nested
properties.
(by the way to make the below code run remark out the foreach block below
//iterate the sub properties)
If anyone could be so kind as to point me in the right direction I would
appreciate it.
Thanks
public void PopulateTreeViewFromObject(TreeView ctrl, object
obj, bool clearControl)
{
TreeNode node;
//clear the tree view control if asked to do so
if(clearControl)
{
ctrl.Nodes.Clear();
}
// Display information for all properties.
foreach(PropertyInfo pi in obj.GetType().GetProperties())
{
node=new TreeNode();
//get the properties of the exception object
PropertyInfo propInfo = (PropertyInfo)pi;
//collect the property info for this node
PopulateTreeViewFromExceptionPropertyInfo(propInfo,node,obj);
//add the node to the control
ctrl.Nodes.Add(node);
}
}
public static void
PopulateTreeViewFromExceptionPropertyInfo(PropertyInfo prop, TreeNode node,
object obj)
{
//collect the property info for this node
node.Text = prop.Name.ToString();
PropertyInfo currentProp =
(obj.GetType().GetProperty(node.Text.ToString()));
if (currentProp.GetValue(obj,null) != null)
{
TreeNode nodeValue = new TreeNode();
nodeValue.Text=currentProp.GetValue(obj,null).ToString();
node.Nodes.Add(nodeValue);
}
//iterate the sub properties
foreach(PropertyInfo pi in
currentProp.GetType().GetProperties())
{
TreeNode subNode=new TreeNode();
//get the properties of the exception object
PropertyInfo subPropInfo = (PropertyInfo)pi;
//collect the property info for this node
PopulateTreeViewFromExceptionPropertyInfo(subPropInfo,subNode,obj);
//add the node to the control
node.Nodes.Add(subNode);
}
}