CommandAsSql/ExtensionMethods.cs
/// <summary>
/// extension method to get a bool value from an object
/// </summary>
/// <param name="o"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
extension method to get a bool value from an object
extension method to get a bool value from an object
public static bool ToBooleanOrDefault(this object o, bool defaultValue)
        {
            bool result = defaultValue;
            if (o != null)
            {
                try
                {
                    switch (o.ToString().ToLower())
                    {
                        case "yes":
                        case "true":
                        case "ok":
                        case "y":
                            result = true;
                            break;
                        case "no":
                        case "false":
                        case "n":
                            result = false;
                            break;
                        default:
                            result = bool.Parse(o.ToString());
                            break;
                    }
                }
                catch
                {
                }
            }
            return result;
        }
public static bool ToBooleanOrDefault ( this object o , bool defaultValue ) { bool result = defaultValue ; if ( o != null ) { try { switch ( o . ToString ( ) . ToLower ( ) ) { case " " : case " " : case " " : case " " : result = true ; break ; case " " : case " " : case " " : result = false ; break ; default : result = bool . Parse ( o . ToString ( ) ) ; break ; } } catch { } } return result ; }
extension method to get a bool value from an object
extension method to get a bool value from an object
None
object
this
defaultValue
bool
None
o
None