将对象转换为String或int c#

时间:2018-01-04 16:20:43

标签: type-conversion

public static int ObjectToInt(object obj)
{
   return ((obj != DBNull.Value && obj != null && Microsoft.VisualBasic.Information.IsNumeric(obj)) ? Convert.ToInt32(obj) : 0);
}

public static string ObjectToString(object obj)
{
   return ((obj != DBNull.Value && obj != null) ? obj.ToString() : "");
}

public static DateTime ObjectToDateTime(object obj)
{
   return (Microsoft.VisualBasic.Information.IsDate(obj) ? Convert.ToDateTime(obj) : new DateTime((long)0));
}

public static bool ObjectToBool(object obj)
{
   return ((obj != DBNull.Value && obj != null) ? (obj != "" ? Convert.ToBoolean(obj) : false) : false);
}

public static decimal ObjectDecimal(object obj)
{
   return ((obj != DBNull.Value && obj != null && Microsoft.VisualBasic.Information.IsNumeric(obj)) ? Convert.ToDecimal(obj) : 0);
}

1 个答案:

答案 0 :(得分:-2)

   public static int ObjectToInt(object obj)
        {
            return ((obj != DBNull.Value && obj != null && Microsoft.VisualBasic.Information.IsNumeric(obj)) ? Convert.ToInt32(obj) : 0);
        }
        public static string ObjectToString(object obj)
        {
            return ((obj != DBNull.Value && obj != null) ? obj.ToString() : "");
        }
        public static DateTime ObjectToDateTime(object obj)
        {
            return (Microsoft.VisualBasic.Information.IsDate(obj) ? Convert.ToDateTime(obj) : new DateTime((long)0));
        }
        public static bool ObjectToBool(object obj)
        {
            return ((obj != DBNull.Value && obj != null) ? (obj != "" ? Convert.ToBoolean(obj) : false) : false);
        }
        public static decimal ObjectDecimal(object obj)
        {
            return ((obj != DBNull.Value && obj != null && Microsoft.VisualBasic.Information.IsNumeric(obj)) ? Convert.ToDecimal(obj) : 0);
        }
  • 列表项