I have a a sql table with 2 columns, excluding PK, etc
DataType Value
--------------------------------------
System.String Demo
System.Int32 23
System.Decimal 184,54
System.DateTime 2018-04-25 08:57:27.6305273
How can I parse dynamically the value from the Value
column, as the type specified in the DataType
column. There can also be different data types: bool, double, etc (Always standard ones. No customs data type so I don't need to fetch assemblies etc)
Of course I can have a dumb solution like:
object value;
if (DataType == "System.String")
{
value = Convert.ToString(XXXX);
}
is there a better generic approach to parse this dynamically?
EDIT:
What do you guys think of this? Is it an overkill ?
Type oType = Type.GetType("System.String");
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(VALUE_IN_HERE)))
{
stream.Seek(0, SeekOriginal.Begin);
var dcs = new DataContractSerializer(oType);
return dcs.ReadObject(stream);
}