Converting String to Object Property Name?

victorhooi

New member
Joined
Apr 29, 2009
Messages
1
Programming Experience
Beginner
heya,

We have an object with properties, and a a SQL database we're reading in, to create a list of objects, with columns corresponding to each property.

If a column is missing from the database (e.g. caused by an outdated version of the database), we want to assign a default value to it.

I've created a dictionary, mapping the column name to its default value (both strings).

However, I was wondering if it was possibly to somehow convert the column name (string) to an object property (e.g. the string "name" to AnObject.name?), as the property and column name are generally the same.

I tried to use GetType().GetPropert("column name"), but I can't seem to do assignments through that)

Alternately, is it possible to somehow create a dictionary, storing the object property as the key (and the column name and default value in a struct), and then grabbing it after.?

Cheers,
Victor
 
When you call GetType().GetProperty() you are getting a PropertyInfo object. That PropertyInfo represents a specific property of a specific type. To use that PropertyInfo to set the property value of a specific instance of that type you call SetValue and specify the instance whose property you want to set and the value you want to set it to.
 
Back
Top