using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Collections;
|
using System.Text.RegularExpressions;
|
|
namespace CY_DocumentSynchroWCFService
|
{
|
/// <summary>
|
/// 数据类型转换类
|
/// </summary>
|
public class MyConvert
|
{
|
#region string数据转换
|
public static bool GetBoolean(string value)
|
{
|
bool result = false;
|
bool.TryParse(value, out result);
|
return result;
|
}
|
|
public static DateTime GetDateTime(string value)
|
{
|
DateTime now = DateTime.Now;
|
DateTime.TryParse(value, out now);
|
return now;
|
}
|
|
public static decimal GetDecimal(string num)
|
{
|
decimal result = 0M;
|
decimal.TryParse(num, out result);
|
return result;
|
}
|
|
public static double GetDouble(string num)
|
{
|
double result = 0.0;
|
double.TryParse(num, out result);
|
return result;
|
}
|
|
public static float GetFloat(string num)
|
{
|
float result = 0f;
|
float.TryParse(num, out result);
|
return result;
|
}
|
|
public static short GetInt16(string num)
|
{
|
short result = 0;
|
short.TryParse(num, out result);
|
return result;
|
}
|
|
public static int GetInt32(string num)
|
{
|
int result = 0;
|
int.TryParse(num, out result);
|
return result;
|
}
|
|
public static long GetInt64(string num)
|
{
|
long result = 0L;
|
long.TryParse(num, out result);
|
return result;
|
}
|
|
#endregion
|
|
#region object数据转换
|
|
|
|
public static int? ConvertToInt(object targetValue)
|
{
|
int? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
int result = 0;
|
int.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static Int32? ConvertToInt32(object targetValue)
|
{
|
Int32? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
else { }
|
|
if (TypeParse.IsNumeric(targetValue))
|
return Int32.Parse(targetValue.ToString());
|
else
|
return null;
|
|
}
|
|
public static Int64? ConvertToInt64(object targetValue)
|
{
|
Int64? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
Int64 result = 0;
|
Int64.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static long? ConvertToLong(object targetValue)
|
{
|
long? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
long result = 0;
|
long.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static Boolean ConvertIntToBoolean(object targetValue)
|
{
|
return targetValue != DBNull.Value ? ("1".Equals(targetValue.ToString()) || true.ToString().Equals(targetValue.ToString())) : false;
|
}
|
/// <summary>
|
/// 转为Boolean
|
/// 2013-5-22 10:00修改实现为:true\"true"\"TRUE"\1 均为true
|
/// </summary>
|
/// <param name="targetValue"></param>
|
/// <returns></returns>
|
public static Boolean ConvertToBoolean(object targetValue)
|
{
|
return targetValue != DBNull.Value ? (true.ToString().ToUpper().Equals(targetValue.ToString().ToUpper()) || "1".Equals(targetValue)) : false;
|
}
|
|
public static Double? ConvertToDouble(object targetValue)
|
{
|
Double? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
Double result = 0;
|
Double.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static decimal? ConvertToDecimal(object targetValue)
|
{
|
decimal? returnValue = null;
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return returnValue;
|
}
|
|
decimal result = 0;
|
decimal.TryParse(targetValue.ToString(), out result);
|
returnValue = result;
|
|
return returnValue;
|
}
|
|
public static DateTime? ConvertToDateTime(object targetValue)
|
{
|
|
if (null == targetValue || targetValue == DBNull.Value)
|
{
|
return null;
|
}
|
else { }
|
|
string targetValueStr = targetValue.ToString();
|
|
if (string.IsNullOrEmpty(targetValueStr)) { return null; } else { }
|
|
DateTime returnValue;
|
DateTime.TryParse(targetValueStr, out returnValue);
|
|
return returnValue;
|
}
|
|
|
public static byte[] ConvertToByte(object targetValue)
|
{
|
return targetValue != DBNull.Value ? targetValue as byte[] : null;
|
}
|
|
public static Guid ConvertToGuid(object targetValue)
|
{
|
Guid outGuid = Guid.Empty;
|
Guid.TryParse(string.Format("{0}", targetValue), out outGuid);
|
return outGuid;
|
|
}
|
public static string ConvertToString(object targetValue)
|
{
|
return string.Format("{0}", targetValue);
|
}
|
#endregion
|
}
|
}
|