Beispiel #1
0
        public static T Convert <T>(object i)
        {
            bool typeErr;

            return(ObjectConv.Convert <T>(i, out typeErr));
        }
Beispiel #2
0
        public static T Convert <T>(object i, out bool typeErr)
        {
            Type type = typeof(T);

            typeErr = false;
            T res;

            if (type == typeof(bool) && IsType <bool>(i))
            {
                res = (T)(object)ObjectConv.Logical(i);
            }
            else
            {
                if (type == typeof(DateTime) && IsType <DateTime>(i))
                {
                    object o;
                    double d;

                    XlCall.XlReturn r = XlCall.TryExcel(XlCall.xlCoerce, out o, i);
                    if (r == XlCall.XlReturn.XlReturnSuccess && double.TryParse(o.ToString(), out d))
                    {
                        res = (T)(object)DateTime.FromOADate(d);
                    }
                    else
                    {
                        typeErr = true;
                        res     = (T)(object)DateTime.MinValue;
                    }
                }
                else
                {
                    if (IsType <double>(i) && type == typeof(int))
                    {
                        double t = (double)i;
                        res = (T)(object)System.Convert.ToInt32(t);
                    }
                    else
                    {
                        if (type == typeof(string) && !(i is ExcelEmpty))
                        {
                            res = (T)(object)i.ToString();
                        }
                        else
                        {
                            if (IsType <T>(i))
                            {
                                res = (T)i;
                            }
                            else
                            {
                                typeErr = (i is ExcelEmpty) ? false : true;
                                if (type == typeof(string))
                                {
                                    res = (T)(object)string.Empty;
                                }
                                else
                                {
                                    res = default(T);
                                }
                            }
                        }
                    }
                }
            }

            return(res);
        }