Obviously this isn't something you would want to do unless you're in a helpless situation, but does anyone have a good example of something like this (bonus points for thinking of a clearer method name):
public static object ConvertToBestGuessPrimitive(string toConvert)
{
if(looksLikeAnInt)
{
return as an int;
}
else if(looksLikeABoolean)
{
return as a boolean;
}
else
{
return as a string;
}
}
The only idea I had was to chain together a bunch of TryParse methods together in a sensible order and fall back to string if nothing works. Can anyone think of something better? This question has probably already been answered somewhere, but I can't seem to find any useful results with the overly generic search terms being used.
Edit - Since there's been some criticism of how useful this would be without an example, here's one I came up with to satisfy a concrete need (however implausible)...Say I am parsing an error log file that I get from an uncontrolled source. The log file has method names and arguments that were provided for where the error occurred. I want to automate testing the error conditions by finding the best matched method and arguments and attempting to call them again. Maybe a stupid example (please don't try and come up with "but you could do this in this scenario" responses since this is just an example), but it illustrates a few points: 1) The input is out of my control. 2) The best-guess is based on some criteria for finding a suitable match. For example: a "10/2/2012" is more likely to mean a DateTime than a string.