通过object获得相关的类型值
2017-10-14 23:53:38 访问(1656) 赞(0) 踩(0)
#region 通过object获得int值 - IntByObj
/// <summary>
/// 通过object获得int值 - IntByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected int IntByObj(object obj)
{
if (obj is int)
return (int)obj;
if (obj == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为null。"
);
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为空。"
);
}
int theResult = 0;
if (int.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效int值。"
);
}
/// <summary>
/// 通过object获得int值 - IntByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected int IntByObj(object obj, int defaultValue)
{
if (obj is int)
return (int)obj;
if (obj == null)
return defaultValue;
string str = obj.ToString().Trim();
if (str.Length == 0)
return defaultValue;
int theResult = 0;
if (int.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得int值 - IntByObj
#region 通过object获得int?值 - IntNullByObj
/// <summary>
/// 通过object获得int?值 - IntByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected int? IntNullByObj(object obj)
{
if (obj is int)
return (int)obj;
if (obj == null)
{
return null;
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
return null;
}
int theResult = 0;
if (int.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效int值。"
);
}
/// <summary>
/// 通过object获得int?值 - IntNullByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected int? IntNullByObj(object obj, int? defaultValue)
{
if (obj is int)
return (int)obj;
if (obj == null)
return null;
string str = obj.ToString().Trim();
if (str.Length == 0)
return null;
int theResult = 0;
if (int.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得int?值 - IntNullByObj
#region 通过object获得long值 - longByObj
/// <summary>
/// 通过object获得long值 - longByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected long LongByObj(object obj)
{
if (obj is long)
return (long)obj;
if (obj == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为null。"
);
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为空。"
);
}
long theResult = 0;
if (long.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效long值。"
);
}
/// <summary>
/// 通过object获得long值 - longByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
protected long LongByObj(object obj, long defaultValue)
{
if (obj is long)
return (long)obj;
if (obj == null)
return defaultValue;
string str = obj.ToString().Trim();
if (str.Length == 0)
return defaultValue;
long theResult = 0;
if (long.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得long值 - longByObj
#region 通过object获得long?值 - LongNullByObj
/// <summary>
/// 通过object获得long?值 - LongNullByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected long? LongNullByObj(object obj)
{
if (obj is long)
return (long)obj;
if (obj == null)
{
return null;
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
return null;
}
long theResult = 0;
if (long.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效long?值。"
);
}
/// <summary>
/// 通过object获得long?值 - LongNullByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
protected long? LongNullByObj(object obj, long? defaultValue)
{
if (obj is long)
return (long)obj;
if (obj == null)
return null;
string str = obj.ToString().Trim();
if (str.Length == 0)
return null;
long theResult = 0;
if (long.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得long?值 - LongNullByObj
#region 通过object获得DateTime值 - DateTimeByObj
/// <summary>
/// 通过object获得DateTime值 - DateTimeByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected DateTime DateTimeByObj(object obj)
{
if (obj is DateTime)
return (DateTime)obj;
if (obj == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为null。"
);
}
string str = obj.ToString();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为空。"
);
}
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效DateTime值。"
);
}
/// <summary>
/// 通过object获得DateTime值 - DateTimeByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
protected DateTime DateTimeByObj(object obj, DateTime defaultValue)
{
if (obj is DateTime)
return (DateTime)obj;
if (obj == null)
return defaultValue;
string str = obj.ToString();
if (str.Length == 0)
return defaultValue;
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得DateTime值 - DateTimeByObj
#region 通过object获得DateTime?值 - DateTimeNullByObj
/// <summary>
/// 通过object获得DateTime?值 - DateTimeNullByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected DateTime? DateTimeNullByObj(object obj)
{
if (obj is DateTime)
return (DateTime)obj;
if (obj == null)
{
return null;
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
return null;
}
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效DateTime?值。"
);
}
/// <summary>
/// 通过object获得DateTime?值 - DateTimeNullByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
protected DateTime? DateTimeNullByObj(object obj, DateTime? defaultValue)
{
if (obj is DateTime)
return (DateTime)obj;
if (obj == null)
return null;
string str = obj.ToString().Trim();
if (str.Length == 0)
return null;
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得DateTime?值 - DateTimeNullByObj
#region 通过object获得double值 - DoubleByObj
/// <summary>
/// 通过object获得double值 - DoubleByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected double DoubleByObj(object obj)
{
if (obj is double)
return (double)obj;
if (obj == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为null。"
);
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为空。"
);
}
double theResult = 0;
if (double.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效double值。"
);
}
/// <summary>
/// 通过object获得double值 - DoubleByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected double DoubleByObj(object obj, double defaultValue)
{
if (obj is double)
return (double)obj;
if (obj == null)
return defaultValue;
string str = obj.ToString().Trim();
if (str.Length == 0)
return defaultValue;
double theResult = 0;
if (double.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得double值 - DoubleByObj
#region 通过object获得double?值 - DoubleNullByObj
/// <summary>
/// 通过object获得double?值 - DoubleByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected double? DoubleNullByObj(object obj)
{
if (obj is double)
return (double)obj;
if (obj == null)
{
return null;
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
return null;
}
double theResult = 0;
if (double.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效double值。"
);
}
/// <summary>
/// 通过object获得double?值 - DoubleNullByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected double? DoubleNullByObj(object obj, double? defaultValue)
{
if (obj is double)
return (double)obj;
if (obj == null)
return null;
string str = obj.ToString().Trim();
if (str.Length == 0)
return null;
double theResult = 0;
if (double.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得double?值 - DoubleNullByObj
#region 通过object获得decimal值 - DecimalByObj
/// <summary>
/// 通过object获得decimal值 - DecimalByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected decimal DecimalByObj(object obj)
{
if (obj is decimal)
return (decimal)obj;
if (obj == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为null。"
);
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为空。"
);
}
decimal theResult = 0;
if (decimal.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效decimal值。"
);
}
/// <summary>
/// 通过object获得decimal值 - DecimalByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected decimal DecimalByObj(object obj, decimal defaultValue)
{
if (obj is decimal)
return (decimal)obj;
if (obj == null)
return defaultValue;
string str = obj.ToString().Trim();
if (str.Length == 0)
return defaultValue;
decimal theResult = 0;
if (decimal.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得decimal值 - DecimalByObj
#region 通过object获得decimal?值 - DecimalNullByObj
/// <summary>
/// 通过object获得decimal?值 - DecimalByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected decimal? DecimalNullByObj(object obj)
{
if (obj is decimal)
return (decimal)obj;
if (obj == null)
{
return null;
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
return null;
}
decimal theResult = 0;
if (decimal.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效decimal值。"
);
}
/// <summary>
/// 通过object获得decimal?值 - DecimalNullByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected decimal? DecimalNullByObj(object obj, decimal? defaultValue)
{
if (obj is decimal)
return (decimal)obj;
if (obj == null)
return null;
string str = obj.ToString().Trim();
if (str.Length == 0)
return null;
decimal theResult = 0;
if (decimal.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得decimal?值 - DecimalNullByObj
#region 通过object获得float值 - FloatByObj
/// <summary>
/// 通过object获得float值 - FloatByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected float FloatByObj(object obj)
{
if (obj is float)
return (float)obj;
if (obj == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为null。"
);
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj"
+ "为空。"
);
}
float theResult = 0;
if (float.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效float值。"
);
}
/// <summary>
/// 通过object获得float值 - FloatByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected float FloatByObj(object obj, float defaultValue)
{
if (obj is float)
return (float)obj;
if (obj == null)
return defaultValue;
string str = obj.ToString().Trim();
if (str.Length == 0)
return defaultValue;
float theResult = 0;
if (float.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得float值 - FloatByObj
#region 通过object获得float?值 - FloatNullByObj
/// <summary>
/// 通过object获得float?值 - FloatByObj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
protected float? FloatNullByObj(object obj)
{
if (obj is float)
return (float)obj;
if (obj == null)
{
return null;
}
string str = obj.ToString().Trim();
if (str.Length == 0)
{
return null;
}
float theResult = 0;
if (float.TryParse(str, out theResult))
return theResult;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:"
+ "object obj[" + str + "]"
+ "为无效float值。"
);
}
/// <summary>
/// 通过object获得float?值 - FloatNullByObj
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认返回值</param>
/// <returns></returns>
protected float? FloatNullByObj(object obj, float? defaultValue)
{
if (obj is float)
return (float)obj;
if (obj == null)
return null;
string str = obj.ToString().Trim();
if (str.Length == 0)
return null;
float theResult = 0;
if (float.TryParse(str, out theResult))
return theResult;
return defaultValue;
}
#endregion 通过object获得float?值 - FloatNullByObj
上一条:
下一条:
相关评论
发表评论