Tip: Propertyinfo.getValue in VB

S

SyZLaB

I had a problem using system.reflection.propertyinfo.getvalue in vb -
and searched a lots of newsgroups and libraries - but none had the
solution to my problem - and it also seamed like many others had the
same problem: system.reflection.propertyinfo.getValue fails with
"Object does not match target type."

As many of the posts where to old to answer, here's the "solution" :
The obj parameter is the object from where propertyinfo gets the
property from - not the property object it self..

To list all values of all properties of type system.string - ex:

Dim str as string ="" ' - some string to send the output to
Dim obj as someobject ' - a object with properties
Dim objProps() As System.Reflection.PropertyInfo =
GetType(obj).GetProperties

For Each p As System.Reflection.PropertyInfo In objProps
If p.PropertyType Is GetType(System.String) Then
str += p.Name.ToString & " = " & p.GetValue(obj,
Nothing).ToString
end if
next
 

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