This would perhaps have been better as a comment to the previous answer but I don't have enough rep yet.
The dictionary and lambdas may be adding overhead to the method so here is an alternative that might start you down the right track.
static string ValueToString(object value)
{
if (value == null)
return "";
Type valueType = value.GetType();
if (valueType == typeof(string))
{
return value as string;
}
if (valueType == typeof(decimal))
{
return ((decimal)value).ToString(
"0.############################",
CultureInfo.InvariantCulture);
}
if (valueType == typeof(DateTime))
{
return ((DateTime)value).Ticks.ToString("X");
}
if (typeof(Enum).IsAssignableFrom(valueType))
{
return ((int)value).ToString("X");
}
return Convert.ToString(value,
CultureInfo.InvariantCulture);
}
One thing that would be interesting to know is whether is the type testing that is slow or the conversion once you know what type you have. My approach has focussed on the testing rather than the conversion. If the latter turned out to be the slow part then you will not get much benefit from this code.
This answerThis answer might give you some insight into the issue with the enum conversion. It rightly points out that you can't convert a variable typed as Enum to int using a direct cast.
Edit: The above comment is a bit strong. The best way to convert an Enum is directly casting it to the underlying type (see this answeranswer). I tested the conversion and Convert.ToInt32 is slower than the direct cast by a little bit.
Interestingly I tested just calling ToString on the enum directly and it appears to be an order of magnitude slower.