object转成long值,如果不符合条件抛出异常

2017-01-30 19:07:26  访问(1746) 赞(1) 踩(0)


        #region object转成long值,如果不符合条件抛出异常

        /// <summary>
        /// object转成long值,如果不符合条件抛出异常
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public long LongConvertTo(object obj)
        {
            if (obj is long)
                return (long)obj;

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

            string str = obj.ToString();

            if (str.Length == 0)
                throw new Exception("obj.ToString().Length == 0");

            long theResult = long.MinValue;

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

            throw new Exception("obj == " + str + " 不是有效的长整型值。");
        }

        #endregion object转成long值,如果不符合条件抛出异常


标签:object转成long值,如果不符合条件抛出异常 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)