GetDefaultBoolConversionValue() public static method

Get default bool conversion value
public static GetDefaultBoolConversionValue ( ) : bool
return bool
示例#1
0
        /// <summary>
        /// Parse string array in bool array
        /// </summary>
        /// <param name="strValue">string to parse</param>
        /// <param name="defaultValue">default value when default tryparse</param>
        /// <param name="allowDefaultConversion">Allow default tryparse values</param>
        /// <returns>bool array</returns>
        public static bool[] TryParseBoolArray(this string strValue, bool[] defaultValue, bool allowDefaultConversion)
        {
            if (String.IsNullOrEmpty(strValue))
            {
                return defaultValue ?? new bool[] {}
            }
            ;

            var boolList = defaultValue != null
                ? defaultValue.ToList()
                : new List <bool>();

            foreach (var l in strValue.Split(','))
            {
                var strBool = l ?? "";

                if (String.IsNullOrEmpty(strBool) || strBool == "-1" || strBool.ToLower() == "undefined")
                {
                    if (allowDefaultConversion)
                    {
                        boolList.Add(BasePrimitivesExtensions.GetDefaultBoolConversionValue());
                    }

                    continue;
                }


                if (strBool == "1" || strBool == "0")
                {
                    boolList.Add(strBool == "1");
                    continue;
                }


                if (strBool.ToLower() == "true" || strBool.ToLower() == "false")
                {
                    boolList.Add(strBool.ToLower() == "true");
                    continue;
                }


                bool boolConvert;
                if (!bool.TryParse(strBool, out boolConvert))
                {
                    if (allowDefaultConversion)
                    {
                        boolList.Add(BasePrimitivesExtensions.GetDefaultBoolConversionValue());
                    }
                }
                else
                {
                    boolList.Add(boolConvert);
                }
            }

            return(boolList.ToArray());
        }
示例#2
0
 /// <summary>
 /// <para>Try parse object to boolean value</para>
 /// <para>Default value is false</para>
 /// </summary>
 /// <param name="objValue">object to convert</param>
 /// <returns>bool result</returns>
 public static bool TryParseBool(this object objValue)
 {
     return(objValue.TryParseBool(BasePrimitivesExtensions.GetDefaultBoolConversionValue()));
 }