List object members and values
I have a 3rd party object that gets passed to one of my methods. The object contains 20 or so string members. How can I e开发者_如何转开发asily list all of the string names and their values?
Are you talking about properties? If so, you can use reflection:
Dim properties = theObject.GetType().GetProperties()
For Each prop In properties
Console.WriteLine("{0}: {1}", prop.Name, _
prop.GetValue(theObject, New Object() { }))
Next
This returns all public properties of the object via GetProperties
.
Use o.GetType().GetProperties()
Then, use the PropertyInfo.PropertyType
property to make sure it's a string,
Then, foreach property, call GetValue (o, null)
props = o.GetType().GetProperties()
PropertyInfo prop = props(0)
Console.WriteLine (prop.Name & " = " & prop.GetValue (o, Nothing))
精彩评论