What I'm doing is looking up a value for a particular field in the hashtable. The object can be a handful of primitive types who's value is destined to be put inside XML but it comes out of the hashtable as an object. So I have the problem of needing to decide what the type is, cast it up and then use that types ToString. It would be nice if I didn't need to cast it but then it will call the ToString on the object type and not the counterpart method on the actual type.
The following code is functionally correct, but I'm not comfortable with it. Perhaps following this comfort path will lead to me being a purist. Either way I would very much appreciate a nicer way to write this if such exists.
public string GetColumnValue(string columnName)
{
object value = item[columnName];
if (value == null)
return string.Empty;
if (value.GetType() == typeof(string))
{
return (string)value;
}
else if (value.GetType() == typeof(double))
{
return ((double)value).ToString();
}
...
}