Traversing Unknown Objects

MaxinA

Member
Joined
May 11, 2007
Messages
15
Location
Canada
Programming Experience
10+
Hi

Does anyone know how I would take an object and traverse all of its parameters in .NET? I want to take an object and convert it to its XML representation. I cannot hard-code this per se, because the interface varies from system to system. I would like to leave everything late-bound if possible..

I would like to take a parameter and produce:

<paramname>{value}</paramname>

Can anyone help?

Thanks

Andrew
 
Steve,

Thank you for your reply...

That is pretty much what I mean.. I tried to implement that, but I am unclear as to what some of the parameters mean..

My Function:
Private Function GenerateXMLBlob(ByRef objObject As Object) As String
Dim strTemp As String = ""

Dim deleg As Type = GetType(objObject).GetEvent("ev").EventHandlerType
Dim invoke As MethodInfo = deleg.GetMethod("Invoke")
Dim pars As ParameterInfo() = invoke.GetParameters()
Dim p As ParameterInfo

For Each p In pars
strTemp = strTemp & (p.Name) & vbCrLf
Next p

Return strTemp

End Function


The dim deleg... line does not like the objObject (which is my instance)
What is "ev"??

If object "x" comes in to this function, I would like to generate an xml representation like this:

<object type="x">
<param1>value</param1>
<param2>value</param2>
<param3>value</param3>
<param4>value</param4>
...{etc}
</object>


Does that help?

Andrew
 
This may help

Here is a sample console application i whipped up from your code. You were on the right track. I used the IO.DirectoryInfo object as my sample to accomplish what you are trying to.



Imports System.Reflection
Module Module1

Sub Main()
Console.WriteLine("Please press enter to continue...")
Dim crl As String = Console.ReadLine()
Dim dir As New System.IO.DirectoryInfo("c:\test")
Dim str As String = GenerateXMLBlob(dir)


Console.WriteLine(str)
Console.ReadLine()
Main()
End Sub



Private Function GenerateXMLBlob(ByVal objObject As Object) As String
Dim strTemp As String = ""

Dim deleg As Type = objObject.GetType()
Dim invoke As MethodInfo = deleg.GetMethod("MoveTo")
Dim pars As ParameterInfo() = invoke.GetParameters()
Dim p As ParameterInfo

For Each p In pars
strTemp = strTemp & p.Name & " as " & p.ParameterType.ToString
Next p

Return strTemp

End Function
End Module


There were a few errors and things that I changed. For example, there is no reason to pass an object by reference unless you need to change its value. If you are only looking to use its value and not modify it then pass it byval. Also, in order to get an objects method info you only need to get its type. As far as all that event stuff after it, there was no need for that. Once you have an objects type you can then access its methods and attributes. Once you have its methods info you can then go through its parameters. Once you loop through the parameters you can get the name of the parameter and the type of parameter that it is which is most likely equally as important as the name.
 
Last edited:
Steve

That looks good, but I get one little issue, the object I am trying to pass does not have a "GetType" method, and is not a standard type, it is an object that can vary in its type.. Should I be casting it like this:

Dim objObject as Object
objObject = m_objMysterObject


and work with that? or will the "Object"'s default gettype method disappear as soon as I cast the object?

Thanks Again
 
Back
Top