Reflecting a Structure

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

Guest

I created a structure with several variables in it.

I wanted to add all the variables of the structure into a listbox. Is there
a way I can do this in some kind of loop rather than having to type in 26
item.add lines. How does the Intellisence figure out and list out the
variables?

Thanks
Brent
 
Brent said:
I created a structure with several variables in it.

I wanted to add all the variables of the structure into a listbox. Is
there
a way I can do this in some kind of loop rather than having to type in 26
item.add lines. How does the Intellisence figure out and list out the
variables?

Thanks
Brent

Dim st As MyStruct
Dim tp As System.Type = st.GetType()
Dim s As String

Console.WriteLine("Definition: " & tp.Name & " - " & tp.ToString())
Console.WriteLine("Members")
Console.WriteLine("-------")

For Each fi As Reflection.FieldInfo In tp.GetFields()
Console.WriteLine(Space(4) & fi.Name & " As " &
fi.MemberType.ToString())
Next

Console.WriteLine()

Console.WriteLine("Methods")
Console.WriteLine("-------")

For Each fi As Reflection.MethodInfo In tp.GetMethods()
Console.WriteLine(Space(4) & fi.Name)
Next
 
Thanks, that works.
I have to ask a follow up question. I thought I could use fi.setvalue to
update an item's value, but it doesn't work.

fi = tp.GetField(Item)
MsgBox(fi.GetValue(InItems)) 'returns the original value
fi.SetValue(InItems, Value.ToString) 'Nothing happens
MsgBox(fi.GetValue(InItems)) 'still returns the original
value

What am I missing here? Is this the right way to do it?

Thanks
Brent
 
Back
Top