Boolean的常用操作
2014-12-31 15:17:39 访问(1992) 赞(0) 踩(0)
/// <summary>
/// bool值转成字符串形式的"1"或"0"
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public string BooleanToString(bool theValue)
{
if (theValue)
return "1";
else
return "0";
}
/// <summary>
/// bool?值转成字符串形式的"","1"或"0"
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public string BooleanNullToString(bool? theValue)
{
if (theValue == null)
return "";
if (theValue == true)
return "1";
else if (theValue == false)
return "0";
else
return "";
}
/// <summary>
/// 通过int值转义获得bool值
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public bool BooleanGetByInt(int theValue)
{
if (theValue == 1)
{
return true;
}
else if (theValue == 0)
{
return false;
}
else
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "theValue == " + theValue.ToString() + "不是有效的bool值。"
);
}
}
/// <summary>
/// 通过int?值转义获得bool?值
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public bool? BooleanNullGetByIntNull(int? theValue)
{
if (theValue == null)
{
return null;
}
if (theValue == 1)
{
return true;
}
else if (theValue == 0)
{
return false;
}
else
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "theValue == " + theValue.ToString() + "不是有效的bool值。"
);
}
}
/// <summary>
/// 通过string值转义获得bool值
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public bool BooleanGetByStr(string theValue)
{
if (theValue == "1")
{
return true;
}
else if (theValue == "0")
{
return false;
}
else
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "theValue == " + theValue + "不是有效的bool值。"
);
}
}
/// <summary>
/// 通过string值转义获得bool?值
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public bool? BooleanNullGetByStr(string theValue)
{
if (theValue == null || theValue.Length == 0)
return null;
if (theValue == "1")
{
return true;
}
else if (theValue == "0")
{
return false;
}
else
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "theValue == " + theValue + "不是有效的bool值。"
);
}
}
/// <summary>
/// object转成bool值
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool BooleanConvertTo(object obj)
{
if (obj == null)
throw new Exception("obj == null");
if (obj is Boolean)
return (bool)obj;
string str = obj.ToString();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "obj.ToString().Length == 0"
);
}
if (str == "1")
{
return true;
}
else if (str == "0")
{
return false;
}
else
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "obj.ToString() == " + str + "不是有效的bool值"
);
}
}
/// <summary>
/// object转成bool值
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public bool BooleanConvertTo(object obj, bool defaultValue)
{
if (obj == null)
return defaultValue;
if (obj is Boolean)
return (bool)obj;
string str = obj.ToString();
if (str.Length == 0)
return defaultValue;
if (str == "1")
return true;
else if (str == "0")
return false;
else
return defaultValue;
}
/// <summary>
/// object转成bool?值
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool? BooleanNullConvertTo(object obj)
{
if (obj == null)
return null;
if (obj is Boolean)
return (bool)obj;
string str = obj.ToString();
if (str.Length == 0)
return null;
if (str == "1")
return true;
else if (str == "0")
return false;
else
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "obj.ToString() == " + str + "不是有效的bool值"
);
}
}
/// <summary>
/// object转成bool?值
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public bool? BooleanNullConvertTo(object obj, bool? defaultValue)
{
if (obj == null)
return null;
if (obj is Boolean)
return (bool)obj;
string str = obj.ToString();
if (str.Length == 0)
return null;
if (str == "1")
return true;
else if (str == "0")
return false;
else
return defaultValue;
}
标签:
Boolean的常用操作 


上一条:
下一条:
相关评论
发表评论