(Using C# .NET, SQL and Hashtables)
It can be particularly cumbersome setting properties in an object, especially if there are a lot of them, and you want to add/remove/change properties over time.
We can use key/value pairs in a Hashtable to populate an object's properties "automatically" by using reflection and some consistent naming of properties and variables.
Specifically, in this example, we will use SQL query field naming (e.g. "SELECT field1 AS Name") to populate a Hashtable, which is then sent to an object's constructor, and the constructor "self populates" its own properties.
The class object
Let's use a simple person class...
Nothing exciting there. However, if we want to pass through a list of key/value pairs (of an unspecified length) to automatically assign properties, we need a second constructor. This constructor is going to take a single argument - a Hashtable.
When we pass a Hastable to create a new Person object, the constructor will loop through each property (via Reflection) and look for that property name in the Hashtable (by comparing the property names with the Hashtable keys); if the property is an integer or boolean, it will provide the relevant casting.
Obviously there could be a good deal more validation here, but it serves the purpose of demonstration.
Getting our data
If we assume that we want to populate a number of Person objects from some pre-existing data (from, say, a database), we can use SQL named parameters:
We can then pass this query to a SqlDataREader object, and loop through each row, adding the name of the column and the value for each row in the query result set.
Such that we now have several Person objects with "automatically" populated properties.