/// <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()); }
/// <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())); }