Reflection???

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I pass an array that consists of elements each of an unknown class to a
function. I want to get a list of the properties of the unknown class and
the value for each property for each element of the array. How would I do
this. I tried propertyDescriptionclass but got an error saying I needed to
use late binding.
 
Something like this:

Dim obj As Object = myArray(1)

For Each pi As PropertyInfo In obj.GetType.GetProperties( _
BindingFlags.Public Or BindingFlags.NonPublic _
Or BindingFlags.Instance)
Console.WriteLine(pi.Name)
Console.WriteLine(pi.GetValue(obj, Nothing).ToString)
Next

This will give you both public and non-public properties. If you only want
the public properties, you can use the overloaded version of GetProperties
without any arguments.

hope that helps..
Imran.
 
I pass an array that consists of elements each of an unknown class to a
function. I want to get a list of the properties of the unknown class and
the value for each property for each element of the array. How would I do
this. I tried propertyDescriptionclass but got an error saying I needed to
use late binding.

hmmm... Here is a rough translation of some code from a debuging class
I wrote that does that (the original is C#, so forgive any little syntax
errors :)

Option Strict On
Option Explicit On

Imports System.Reflection

Dim theType As Type = theObject.GetType ()
Dim properties() As PropertyInfo = theType.GetProperties ()

For Each prop As PropertyInfo in properties
Try
Console.WriteLine ("{0} = {1}", _
prop.Name, prop.GetValue (theObject, Nothing))
Catch
End Try
Next prop

HTH
 
Thanks to both Imran and Tom...it helps a lot.

Tom Shelton said:
hmmm... Here is a rough translation of some code from a debuging class
I wrote that does that (the original is C#, so forgive any little syntax
errors :)

Option Strict On
Option Explicit On

Imports System.Reflection

Dim theType As Type = theObject.GetType ()
Dim properties() As PropertyInfo = theType.GetProperties ()

For Each prop As PropertyInfo in properties
Try
Console.WriteLine ("{0} = {1}", _
prop.Name, prop.GetValue (theObject, Nothing))
Catch
End Try
Next prop

HTH
--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 0 Days, 22 Hours, 23 Minutes, 14 Seconds
 

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

Back
Top