Exemple #1
0
        public static T To <T>(this object text)
        {
            if (text == null)
            {
                return(default(T));
            }
            if (text.Equals(DBNull.Value))
            {
                return(default(T));
            }
            if (text is string)
            {
                if (string.IsNullOrWhiteSpace(text as string))
                {
                    return(default(T));
                }
            }

            var type = typeof(T);

            if (type.ToString() == "QuantLib.Date")
            {
                var           dt   = (DateTime)text;
                QuantLib.Date date = new QuantLib.Date((int)dt.ToOADate());
                return((T)Convert.ChangeType(date, type));
            }

            var underlyingType = Nullable.GetUnderlyingType(type) ?? type;

            return((T)Convert.ChangeType(text, underlyingType));
        }
Exemple #2
0
        public static T ToDatetime <T>(this QuantLib.Date date)
        {
            DateTime dt   = Convert.ToDateTime(date.month() + " " + date.dayOfMonth().ToString() + ", " + date.year().ToString());
            var      type = typeof(T);

            if (type == typeof(string))
            {
                return((T)Convert.ChangeType(dt.ToShortDateString(), typeof(T)));
            }
            else
            {
                return((T)Convert.ChangeType(dt, typeof(T)));
            }
        }