Object - ToBooleanOrDefault
An object extension method that converts this object to a boolean or default.
public static void Main() { object thisValid = true; object thisInvalid = "FizzBuzz"; string format = "{0, -20} {1, -15}"; Console.WriteLine(format, "Object", "ToBooleanOrDefault"); Console.WriteLine(format, "------", "------------------"); // C# Extension Method: Object - ToBooleanOrDefault Console.WriteLine(format, thisValid, thisValid.ToBooleanOrDefault()); Console.WriteLine(format, thisInvalid, thisInvalid.ToBooleanOrDefault()); Console.WriteLine(format, thisInvalid, thisInvalid.ToBooleanOrDefault(false)); Console.WriteLine(format, thisInvalid, thisInvalid.ToBooleanOrDefault(() => false)); }
View Source
using System; public static partial class Extensions { /// <summary> /// An object extension method that converts this object to a boolean or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <returns>The given data converted to a bool.</returns> public static bool ToBooleanOrDefault(this object @this) { try { return Convert.ToBoolean(@this); } catch (Exception) { return default(bool); } } /// <summary> /// An object extension method that converts this object to a boolean or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValue">true to default value.</param> /// <returns>The given data converted to a bool.</returns> public static bool ToBooleanOrDefault(this object @this, bool defaultValue) { try { return Convert.ToBoolean(@this); } catch (Exception) { return defaultValue; } } /// <summary> /// An object extension method that converts this object to a boolean or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValue">true to default value.</param> /// <param name="useDefaultIfNull">true to use default if null.</param> /// <returns>The given data converted to a bool.</returns> public static bool ToBooleanOrDefault(this object @this, bool defaultValue, bool useDefaultIfNull) { if (useDefaultIfNull && @this == null) { return defaultValue; } try { return Convert.ToBoolean(@this); } catch (Exception) { return defaultValue; } } /// <summary> /// An object extension method that converts this object to a boolean or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <returns>The given data converted to a bool.</returns> public static bool ToBooleanOrDefault(this object @this, Func<bool> defaultValueFactory) { try { return Convert.ToBoolean(@this); } catch (Exception) { return defaultValueFactory(); } } /// <summary> /// An object extension method that converts this object to a boolean or default. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <param name="useDefaultIfNull">true to use default if null.</param> /// <returns>The given data converted to a bool.</returns> public static bool ToBooleanOrDefault(this object @this, Func<bool> defaultValueFactory, bool useDefaultIfNull) { if (useDefaultIfNull && @this == null) { return defaultValueFactory(); } try { return Convert.ToBoolean(@this); } catch (Exception) { return defaultValueFactory(); } } }