NewbieFromHell
NewbieFromHell

Reputation: 11

Loop through properties vb.net

Evening All,

I am extracting data from a API which exposes records as properties of the class so an example would be

For Each entry As Webservice.Names In objResponse.results
    string = string & "'firstname'=" & entry.firstname
    string = string & "'surname'=" & entry.surname
next

now we have about 50 entities in the webservice, and each has at least 10 "fields" my question is can I expose properties any other way ?

i.e. could i

For Each entry As Webservice.Names In objResponse.results
    for each prop in entry.properties
         string = string & "'" & prop.name & "'=" & entry.fields(prop.name)
    next
next

or similar, i can't seem to expose the properties above, but really looking for a way that i don't have to hand type every property against the string for inserting to the database.

any thoughts on how to achieve this within VB if the properties aren't exposed as above example

Thanks In Advance

Upvotes: 1

Views: 235

Answers (1)

mehrdad safa
mehrdad safa

Reputation: 1071

You can get list of properties and their values using Reflection.

For Each entry As Webservice.Names In objResponse.results  
    for each prop in entry.GetType().GetProperties()
 ' use System.Reflection.BindingFlags to filter it.   
         string = string & "'" & prop.name & "'=" & prop.GetValue(entry,nothing) 
 ' use entry as first argument. it requires an instance of object to get 'property value  
   next  
next  

Upvotes: 1

Related Questions