开发者

List object members and values

开发者 https://www.devze.com 2023-03-12 09:05 出处:网络
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 ta

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))
0

精彩评论

暂无评论...
验证码 换一张
取 消