playing with Reflection and Exceptions, need a little help

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

}

}
 
M

Marina

Well, you need to recursively do what you are doing for every property.
Just be careful you don't end up doing work forever, as all the primitive
types have their own properties, so you could potentially end up calling
things recursively forever.
 
N

Nicholas Paldino [.NET/C# MVP]

Torre,

I didn't get a chance to run your code, but I am curious, instead of
writing your own property browser, why not use the one that is included in
..NET? It will do the same thing, and give your app a more consistent look.

Of course, if you are doing this as an exercise, then that is fine as
well, just say so, and I'll look at the code.

If you want to use the property browser, you can right click on the
toolbox and select "customize". In the dialog that pops up, you can find
the PropertyInfo control and then check it, and it will be in your toolbox.
Then, for your exception, just set the SelectedObject property to the
Exception, and it should work.

Hope this helps.
 
T

Torre Quinn

I was unaware of the control you mentioned, thank you I will play with it as
a comparison. I was in fact just running this as an exercise. I did
obviously build a UI to display the result but this is not for any work
related project. I was just trying to get a better understanding how I can
use reflection to expose the type and values of an object programmatically.
I honestly have no idea where I would use this.



I was just curious how I could expose those "sub-properties". I could not
find any resources on the net that went that far most just stopped at the
first "tier" of properties.



This is all just in fun, I was only trying to expand my knowledge in an area
that I am, at best, fuzzy on. Thanks







Nicholas Paldino said:
Torre,

I didn't get a chance to run your code, but I am curious, instead of
writing your own property browser, why not use the one that is included in
.NET? It will do the same thing, and give your app a more consistent look.

Of course, if you are doing this as an exercise, then that is fine as
well, just say so, and I'll look at the code.

If you want to use the property browser, you can right click on the
toolbox and select "customize". In the dialog that pops up, you can find
the PropertyInfo control and then check it, and it will be in your toolbox.
Then, for your exception, just set the SelectedObject property to the
Exception, and it should work.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Torre Quinn said:
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);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top