Extensions - HttpRequest, Form, & QueryString 12/9/2009 All Extensions posts

Finally rounded out a solution to get values in a generic way from the QueryString, Form, and HttpRequest. Some cases are there for ASP.Net and how it handles CheckBoxes. Applying this process to the ViewData is the same.
 
Utility class for doing basic conversion
 
        internal static T Get(object input)
        {
            return Get(input, default(T));
        }

        internal static T Get(object input, T defaultValue)
        {
            T value = defaultValue;

            if (input != null)
            {
                if (input.ToString().Replace(" ", string.Empty).ToLower() == "true,false")
                {
                    string[] inputs = input.ToString().Split(',');
                    bool value0 = Boolean.Parse(inputs[0]);
                    bool value1 = Boolean.Parse(inputs[1]);

                    value = (T)Convert.ChangeType(value0 || value1, typeof(T));
                }
                else
                {
                        if (typeof(T).IsEnum)
                        {
                            int v = 0;
                            if (int.TryParse(input.ToString(), out v))
                                value = (T)Convert.ChangeType(input, typeof(int));
                            else
                                value = (T)Enum.Parse(typeof(T), input.ToString());
                        }
                        else if (typeof(T) == typeof(bool))
                        {
                            string v = input.ToString().ToLower();

                            if (v == "1" || v == "true")
                                value = (T)Convert.ChangeType("true", typeof(bool));
                            else if (v == "0" || v == "false")
                                value = (T)Convert.ChangeType("false", typeof(bool));
                        }
                        else if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                        {
                            NullableConverter nc = new NullableConverter(typeof(T));
                            Type underlyingType = nc.UnderlyingType;

                            if (underlyingType.IsEnum)
                            {
                                int v = 0;
                                if (int.TryParse(input.ToString(), out v))
                                    value = (T)Convert.ChangeType(input, typeof(int));
                                else
                                    value = (T)Enum.Parse(typeof(T), input.ToString());
                            }
                            else if (underlyingType == typeof(bool))
                            {
                                string v = input.ToString().ToLower();

                                if (v == "1" || v == "true")
                                    value = (T)Convert.ChangeType("true", typeof(bool));
                                else if (v == "0" || v == "false")
                                    value = (T)Convert.ChangeType("false", typeof(bool));
                            }
                            else
                            {
                                 value = (T)Convert.ChangeType(input, underlyingType);
                            }
                            }
                        }
                        else
                            value = (T)Convert.ChangeType(input, typeof(T));
                }            
            }
            return value;
        }
 
Extension on the QueryString and Form via the NameValueCollection
 
    public static partial class Extensions
    {
        public static T Get(this NameValueCollection collection, string key)
        {
            return GetValue.Get(collection[key]);
        }

        public static T Get(this NameValueCollection collection, string key, T defaultValue)
        {
            return GetValue.Get(collection[key], defaultValue);
        }
    }
 
Extension on the HttpRequest
 
    public static partial class Extensions
    {
        public static T Get(this HttpRequest request, string key)
        {
            return GetValue.Get(request[key]);
        }

        public static T Get(this HttpRequest request, string key, T defaultValue)
        {
            return GetValue.Get(request[key], defaultValue);
        }
    }