object转成int值

2014-12-31 18:36:01  访问(1763) 赞(0) 踩(0)


        /// <summary>
        /// object转成int值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int IntConvertTo(object obj)
        {
            if (obj is int)
                return (int)obj;

            if (obj == null)
                throw new Exception("obj == null");

            string str = obj.ToString();

            if (str.Length == 0)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "obj.ToString().Length == 0"
                    );
            }

            int theResult = int.MinValue;

            if (int.TryParse(str, out theResult))
                return theResult;

            throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "obj == " + str + " 不是有效的整型值。"
                    );
        }


标签:object转成int值 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)