I know differently than doing:
object x; if (theType==typeof(int)) x = int.parse(myString); else if (theType==typeof(bool)) x = bool.Parse(myString);
Also note that serialized data must contain type names, since otherwise you have no way to find out if the type is "123", the type is int or unsigned short, or something else, or, for example, "Red" is an enumeration value, Color object or string.
You can use XmlSerializer or BinaryFormatter to serialize / deserialize your objects, which makes all of this logic right for you.
source share