[转]高效的object转int

        /// <summary>
        /// MS
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int ToIn32(object o)
        {
            if (o != null)
            {
                return ((IConvertible)o).ToInt32(null);
            }
            return 0;
        }

        /// <summary>
        /// CSDN
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        static int ToInt(object o)
        {
            if (o is int)
                return (int)o;
            else if (o is short)
                return (int)(short)o;
            else if (o is byte)
                return (int)(byte)o;
            else if (o is long)
                return (int)(long)o;
            else if (o is double)
                return (int)(double)o;
            else if (o is float)
                return (int)(float)o;
            else if (o is decimal)
                return (int)(decimal)o;
            else if (o is uint)
                return (int)(uint)o;
            else if (o is ushort)
                return (int)(ushort)o;
            else if (o is ulong)
                return (int)(ulong)o;
            else if (o is sbyte)
                return (int)(sbyte)o;
            else
                return (int)double.Parse(o.ToString());
        }

http://msdn.microsoft.com/zh-cn/library/system.iconvertible.aspx

posted on 2012-10-16 14:52  YuanSong  阅读(1839)  评论(0编辑  收藏  举报

导航