.Net中数据类型的安全转换
数据转换错误是大家经常犯的,本没有什么,但数据多了可以影响到开发速度,其实这样的错误是可以避免的,下面是写的一个数据安全转换的通用类,大家可以指点一下:
安全转换SaveChangeData
using System;
using System.Collections.Generic;
using System.Text;
namespace CommonHELP
{
public static class SaveChangeData
{
public static readonly DateTime NULLDateTime = new DateTime(1753, 1, 1, 12, 0, 0);
/// <summary>
/// 布尔转换成字符
/// </summary>
/// <param name="bol"></param>
/// <returns></returns>
public static string BoolToString(bool bol)
{
if (bol)
return "1";
else
return "0";
}
/// <summary>
/// 布尔转换成字符
/// </summary>
/// <param name="bol"></param>
/// <returns></returns>
public static bool StringToBool(string bol)
{
if (bol == "1")
return true;
else
return false;
}
/// <summary>
/// 对象安全转换成字符串,为空返回string.Empty
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ObjectToString(object obj)
{
if (obj != null)
{
return obj.ToString();
}
return string.Empty;
}
/// <summary>
/// 对象安全转换短时间字符串,为空返回 ""
/// </summary>
/// <param name="obj"></param>
/// <param name="isFull"></param>
/// <returns></returns>
public static string GetDateTimeString(object obj, bool isFull)
{
DateTime time = ObjectToDateTime(obj);
if (time == NULLDateTime)
{
return "";
}
else
{
if (isFull)
{
return time.ToString();
}
else
{
return time.ToShortDateString();
}
}
}
/// <summary>
/// 对象安全转换成时间
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static DateTime ObjectToDateTime(object obj)
{
if (obj != null)
{
DateTime dt = new DateTime();
DateTime.TryParse(obj.ToString(), out dt);
return NullDateTime(dt);
}
return NULLDateTime;
}
/// <summary>
/// 对象安全转换成时间 为空则返回 NULL
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static DateTime? ObjectToDateTime2(object obj)
{
if (obj != null)
{
DateTime dt = new DateTime();
DateTime.TryParse(obj.ToString(), out dt);
if (dt != DateTime.MinValue)
return dt;
else
return null;
}
return null;
}
/// <summary>
/// 对象安全转换成整数,如果为空返回 0
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static int ObjectToInt(object obj)
{
if (obj != null)
{
int i = 0;
int.TryParse(obj.ToString(), out i);
return i;
}
return 0;
}
/// <summary>
/// 对象安全转换成整数,如果为空返回 0
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool ObjectToBool(object obj)
{
if (obj != null)
{
bool i = false;
bool.TryParse(obj.ToString(), out i);
return i;
}
return false;
}
/// <summary>
/// 对象安全转换成整数,如果为空返回 -1
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static int NObjectToInt(object obj)
{
if (obj != null)
{
int i = 0;
int.TryParse(obj.ToString(), out i);
return i;
}
return -1;
}
/// <summary>
/// 对象安全转换成双精度小数,为空返回 0d
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static double ObjectToDouble(object obj)
{
if (obj != null)
{
double d = 0d;
double.TryParse(obj.ToString(), out d);
return d;
}
return 0d;
}
/// <summary>
/// 对象安全转换成decimal,为空返回 0d
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static decimal ObjectToDecimal(object obj)
{
if (obj != null)
{
decimal m = 0m;
decimal.TryParse(obj.ToString(), out m);
return m;
}
return 0m;
}
/// <summary>
/// 对象安全转换成单精度小数,为空返回 0f
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static float ObjectToFloat(object obj)
{
if (obj != null)
{
float f = 0f;
float.TryParse(obj.ToString(), out f);
return f;
}
return 0f;
}
/// <summary>
/// 对象安全转换成双精度小数,为空返回 0d
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private static DateTime NullDateTime(DateTime dt)
{
if (dt == DateTime.MinValue)
return NULLDateTime;
return dt;
}
}
}