コード例 #1
0
        public static T To <T>(this IConvertible value, IConvertible ifError)
        {
            try
            {
                Type t = typeof(T);
                Type u = Nullable.GetUnderlyingType(t);

                if (u != null)
                {
                    if (value == null || value.Equals(""))
                    {
                        return((T)ifError);
                    }

                    return((T)Convert.ChangeType(value, u));
                }
                else
                {
                    if (value == null || value.Equals(""))
                    {
                        return((T)(ifError.To <T>()));
                    }

                    return((T)Convert.ChangeType(value, t));
                }
            }
            catch
            {
                return((T)ifError);
            }
        }