Dynamic Casting with .NET

added by mceranski
3/29/2010 7:19:02 AM

0 Kicks, 71 Views

I recently was working on a project where I needed to store settings or various data types into a database table. Since most of the settings were singular I ended up with a table that had two columns which were titled name and value. The name and value field of the Setting table are both defined as varchars ( or strings). By defining the value field as a string, I can serialize any object to the table by using the ToString() method. Over time my application could grow to have dozens of settings of varying data types. So I needed a mechanism to load the settings from the table and assign them to an object. While loading each setting, I need to dynamically determine the data type of each field and dynamically cast it to the correct type.