Jackie,
This is a very common requirement in the Web Development world and as you've noted there is no built-in way to achieve this and what is there falls short in the simplest of cases.
@Chris these rules are not arbitrary by any shot of the imagination. They are in fact very common when it comes to dealing with user input, especially over the web. In fact the Boolean conversions are also pretty common given that check boxes in ASP.NET return on/off (for some reason).
You have a a couple of options. One is a simplistic approach and another an extensible solution. It all starts from the way you'd like to use this functionality from within your application. Since you've not shed a lot of light on what it is you are doing currently or how you'd like to do this, I've taken the liberty of making a few assumptions.
The primary assumption being that the values come to you via the Request.Form or Request.QueryStrings (both of which are NameValueCollections that can hold multiple values for a given name).
Let's assume you'd like a method called ChangeType that given a name of the parameters and the Type you'd like to change it to will return the value as the required type. So the method signature might look like this:
public T ChangeType<T>(string parameterName, T defaultValue = default(T))
And so you can use it like so:
int someId = ChangeType<int>("id", -1);
So the value of the variable someId will either be the value extracted from Request.Form or Request.QueryStrings in the form of an int (if it exists) or -1 if it does not.
A more complete implementation is as follows:
static T ChangeType<T>(string value) where T: struct
{
Type typeOft = typeof(T);
if (String.IsNullOrEmpty(value.Trim()))
return default(T);
if (typeOft == typeof(Int32))
return (T)ConvertToInt32(value);
else if (typeOft == typeof(Int64))
return (T)ConvertToInt64(value);
else if (typeOft == typeof(Double))
return (T)ConvertToDouble(value);
else if (typeOft == typeof(Decimal))
return (T)ConvertToDecimal(value);
return default(T);
}
static object ConvertToInt32(string value)
{
return Int32.Parse(value,
NumberStyles.Currency ^ NumberStyles.AllowDecimalPoint);
}
static object ConvertToInt64(string value)
{
return Int64.Parse(value,
NumberStyles.Currency ^ NumberStyles.AllowDecimalPoint);
}
static object ConvertToDouble(string value)
{
return Double.Parse(value, NumberStyles.Currency);
}
static object ConvertToDecimal(string value)
{
return Decimal.Parse(value, NumberStyles.Currency);
}
If you need to be able to handle more types, simply implement the required method (such as ConvertToInt32 for example) and add another else condition in the ChangeType<T>
method and you're done.
Now if you're looking for an extensible solution such that you can add additional capability without having to modify the primary code and be able ot handle your own custom types, then please take a look at this blog post of mine.
[ChangeType – Changing the type of a variable in C#][1]
http://www.matlus.com/2010/11/changetypet-changing-the-type-of-a-variable-in-c/
Hope this Helps.