For testing purposes, I'd like to see all of the properties and their corresponding values of an HttpApplication
object (I'm testing some functionality of an HTTPModule). My first thought was to serialize it to XML, then view that or write it to a file.
The problem is, HttpApplication
is not a serializable class, so an exception is thrown when I try to serialize. Are there any other techniques, or is it even possible to get a string representation of a non-serializable object? I'd just like to see all of the same properties I get with Intellisense and their values.
I've seen some articles which mention Reflection, but I haven't found anything that suggests it would work for my scenario.
UPDATE:
After getting a couple responses, it looks like I'll need to use Reflection. Here is the code I'm using:
Dim sProps As New StringBuilder
For Each p As System.Reflection.PropertyInfo In oHttpApp.GetType().GetProperties()
If p.CanRead Then
sProps.AppendLine(p.Name & ": " & p.GetValue(oHttpApp, Nothing))
End If
Next
On my AppendLine statement, an exception is thrown right away:
System.InvalidCastException: Operator '&' is not defined for string "Context: " and type 'HttpContext'. at Microsoft.VisualBasic.CompilerServices.Operators.InvokeObjectUserDefinedOperator(UserDefinedOperator Op, Object[] Arguments) at Microsoft.VisualBasic.CompilerServices.Operators.InvokeUserDefinedOperator(UserDefinedOperator Op, Object[] Arguments) at Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject(Object Left, Object Right)
@granadaCoder, you mentioned that I'll need to know how "deep" to go, I'm wondering if this is the problem. In the error above, Context
is an complex object so I would need to drill into that and get its individual properties, correct? Do you know how I might be able to do that - or would it be as simple as calling GetProperties
again on p
inside my loop?