Boolean相关的操作

2015-12-07 14:50:06  访问(1715) 赞(0) 踩(0)

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using SlowX.Functions.Common;


namespace SlowX.Functions.Functions
{
    /// <summary>
    /// Boolean
    /// </summary>
    public class BooleanSlowXFunctions
    {
        /// <summary>
        /// typeof(bool)
        /// </summary>
        public readonly static Type boolType = typeof(bool);

        /// <summary>
        /// typeof(Boolean)
        /// </summary>
        public readonly static Type BooleanType = typeof(Boolean);


        /// <summary>
        /// 构造函数
        /// </summary>
        public BooleanSlowXFunctions()
        {
            
        }

        /// <summary>
        /// 获得boolean值
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <param name="theFlag"></param>
        /// <returns></returns>
        public static bool GetBooleanByInt(bool defaultValue,int theFlag )
        {
            if (theFlag == CommonValueSlowXFunctionsCommon.TRUE)
                return true;
            else if (theFlag == CommonValueSlowXFunctionsCommon.FALSE)
                return false;
            else
                return defaultValue;
        }

        /// <summary>
        /// 获得boolean值
        /// </summary>
        /// <param name="theFlag"></param>
        /// <returns></returns>
        public static bool GetBooleanByString(string theFlag)
        {
            if (theFlag == CommonValueSlowXFunctionsCommon.STR_TRUE)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获得int值
        /// </summary>
        /// <param name="bFlag"></param>
        /// <returns></returns>
        public static int GetIntByBoolean(bool bFlag)
        {
            if (bFlag)
                return CommonValueSlowXFunctionsCommon.TRUE;
            else
                return CommonValueSlowXFunctionsCommon.FALSE;
        }

        /// <summary>
        /// 获得string值
        /// </summary>
        /// <param name="bFlag"></param>
        /// <returns></returns>
        public static string GetStrByBoolean(bool bFlag)
        {
            if (bFlag)
                return CommonValueSlowXFunctionsCommon.STR_TRUE;
            else
                return CommonValueSlowXFunctionsCommon.STR_FALSE;

            
        }

        /// <summary>
        /// 获得string值
        /// </summary>
        /// <param name="bFlag"></param>
        /// <returns></returns>
        public static string GetStrByBooleanNull(bool? bFlag)
        {
            if (bFlag == null)
                return "";

            if (bFlag == true)
                return CommonValueSlowXFunctionsCommon.STR_TRUE;

            if (bFlag == false)
                return CommonValueSlowXFunctionsCommon.STR_FALSE;

            throw new Exception
                (
                    "方法:"
                    + MethodBase.GetCurrentMethod().ReflectedType.FullName
                    + " "
                    + MethodBase.GetCurrentMethod().ToString()
                    + " 发生异常:" + "bool? bFlag 值未知。");
        }

        /// <summary>
        /// 如果 theValue == null,返回 defaultValue
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool GetBooleanByBooleanNull(bool? theValue, bool defaultValue)
        {
            if (theValue == null)
                return defaultValue;

            return (bool)theValue;
        }

        /// <summary>
        /// 获得“是”或“否”
        /// </summary>
        /// <param name="theFlag"></param>
        /// <returns></returns>
        public static string GetCnStrByBoolean(bool theFlag)
        {
            if (theFlag)
                return "是";
            else
                return "否";
        }


        /// <summary>
        /// 显示值
        /// </summary>
        /// <param name="theFlag"></param>
        /// <param name="trueText"></param>
        /// <param name="falseText"></param>
        /// <returns></returns>
        public static string GetStrTextByBoolean(bool theFlag, string trueText, string falseText)
        {
            if (theFlag)
                return trueText;
            else
                return falseText;
        }

        /// <summary>
        /// 获得Boolean相关的值
        /// </summary>
        /// <param name="oValue"></param>
        /// <returns></returns>
        public static bool? GetBooleanNullByObj(object oValue)
        {
            if (oValue == null || oValue == DBNull.Value)
                return null;

            string str = oValue.ToString();

            if (str.Length == 0)
                return null;

            str = str.Trim();

            if (str.Length == 0)
                return null;

            if (str == CommonValueSlowXFunctionsCommon.STR_TRUE)
                return true;

            if (str == CommonValueSlowXFunctionsCommon.STR_FALSE)
                return false;

            throw new Exception
                (
                    "方法:"
                    + MethodBase.GetCurrentMethod().ReflectedType.FullName
                    + " "
                    + MethodBase.GetCurrentMethod().ToString()
                    + " 发生异常:" + "str == " + str + " 不是有效的Boolean值。"
                );
        }

        /// <summary>
        /// 获得Boolean相关的值
        /// </summary>
        /// <param name="oValue"></param>
        /// <returns></returns>
        public static bool GetBooleanByObj(object oValue)
        {
            if (oValue == null || oValue == DBNull.Value)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "传入参数:object oValue为null。"
                    );
            }

            if (oValue is Boolean)
                return (bool)oValue;

            string str = oValue.ToString();

            if (str.Length == 0)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "传入参数:object oValue为null。"
                    );
            }

            str = str.Trim();

            if (str.Length == 0)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "传入参数:object oValue为null。"
                    );
            }

            if (str == CommonValueSlowXFunctionsCommon.STR_TRUE)
                return true;

            if (str == CommonValueSlowXFunctionsCommon.STR_FALSE)
                return false;


            throw new Exception
                (
                    "方法:"
                    + MethodBase.GetCurrentMethod().ReflectedType.FullName
                    + " "
                    + MethodBase.GetCurrentMethod().ToString()
                    + " 发生异常:" + "str == " + str + " 不是有效的Boolean值。"
                );
            
        }

        /// <summary>
        /// 获得Boolean的值
        /// </summary>
        /// <param name="oValue"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool GetBooleanByObjNullToDefault(object oValue, bool defaultValue)
        {

            if (oValue == null || oValue == DBNull.Value)
                return defaultValue;

            if (oValue is Boolean)
                return (bool)oValue;

            string str = oValue.ToString();

            if (str.Length == 0)
                return defaultValue;

            str = str.Trim();

            if (str.Length == 0)
                return defaultValue;

            if (str == CommonValueSlowXFunctionsCommon.STR_TRUE)
                return true;

            if (str == CommonValueSlowXFunctionsCommon.STR_FALSE)
                return false;

            throw new Exception
                (
                    "方法:"
                    + MethodBase.GetCurrentMethod().ReflectedType.FullName
                    + " "
                    + MethodBase.GetCurrentMethod().ToString()
                    + " 发生异常:" + "str == " + str + " 不是有效的Boolean值。"
                );
        }

        /// <summary>
        /// bool? 转成 bool 
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static bool BooleanNullToBoolean(bool? theValue,bool defaultValue)
        {
            if (theValue == null)
                return defaultValue;

            return (bool)theValue;
        }

        /// <summary>
        /// 获得Boolean值
        /// </summary>
        /// <param name="theFlag"></param>
        /// <returns></returns>
        public static bool GetBooleanByInt(int theFlag)
        {
            if (theFlag == CommonValueSlowXFunctionsCommon.TRUE)
                return true;
            else if (theFlag == CommonValueSlowXFunctionsCommon.FALSE)
                return false;
            else
                throw new Exception
                (
                    "方法:"
                    + MethodBase.GetCurrentMethod().ReflectedType.FullName
                    + " "
                    + MethodBase.GetCurrentMethod().ToString()
                    + " 发生异常:" + "theFlag = " + theFlag.ToString() + " 为未知Boolean值。");
        }
    }
}


标签:Boolean相关的操作 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)