|
using System;
|
|
namespace Common
|
{
|
public class TypeConvert
|
{
|
public static short ToInt16(object p)
|
{
|
return TypeConvert.ToInt16(p, (short) 0);
|
}
|
|
public static short ToInt16(object p, short defaultValue)
|
{
|
try
|
{
|
return Convert.ToInt16(p);
|
}
|
catch
|
{
|
return defaultValue;
|
}
|
}
|
|
public static int ToInt32(object p)
|
{
|
return TypeConvert.ToInt32(p, 0);
|
}
|
|
public static int ToInt32(object p, int defaultValue)
|
{
|
try
|
{
|
return Convert.ToInt32(p);
|
}
|
catch
|
{
|
return defaultValue;
|
}
|
}
|
|
public static long ToInt64(object p)
|
{
|
return TypeConvert.ToInt64(p, 0);
|
}
|
|
public static long ToInt64(object p, int defaultValue)
|
{
|
try
|
{
|
return (long) Convert.ToInt32(p);
|
}
|
catch
|
{
|
return (long) defaultValue;
|
}
|
}
|
|
public static bool ToBoolean(object p)
|
{
|
return TypeConvert.ToBoolean(p, false);
|
}
|
|
public static bool ToBoolean(object p, bool defaultValue)
|
{
|
try
|
{
|
return Convert.ToBoolean(p);
|
}
|
catch
|
{
|
return defaultValue;
|
}
|
}
|
|
public static string ToString(object p)
|
{
|
return p == null ? "" : p.ToString();
|
}
|
|
public static string ToString(object p, string defaultValue)
|
{
|
return p == null ? defaultValue : p.ToString();
|
}
|
|
public static DateTime ToDateTime(object p)
|
{
|
return TypeConvert.ToDateTime(p, DateTime.MinValue);
|
}
|
|
public static DateTime ToDateTime(object p, DateTime defaultValue)
|
{
|
try
|
{
|
return Convert.ToDateTime(p);
|
}
|
catch
|
{
|
return defaultValue;
|
}
|
}
|
|
public static Decimal ToDecimal(object p)
|
{
|
return TypeConvert.ToDecimal(p, new Decimal(0));
|
}
|
|
public static Decimal ToDecimal(object p, Decimal defaultValue)
|
{
|
try
|
{
|
return Convert.ToDecimal(p);
|
}
|
catch
|
{
|
return defaultValue;
|
}
|
}
|
|
public static double ToDouble(object p)
|
{
|
return TypeConvert.ToDouble(p, 0.0);
|
}
|
|
public static double ToDouble(object p, double defaultValue)
|
{
|
try
|
{
|
return Convert.ToDouble(p);
|
}
|
catch
|
{
|
return defaultValue;
|
}
|
}
|
|
public static float ToFloat(object p)
|
{
|
return TypeConvert.ToFloat(p, 0.0f);
|
}
|
|
public static float ToFloat(object p, float defaultValue)
|
{
|
try
|
{
|
return Convert.ToSingle(p);
|
}
|
catch
|
{
|
return defaultValue;
|
}
|
}
|
}
|
}
|