Object - IsValidBoolean

An object extension method that query if '@this' is valid bool.

Try it

public static void Main()
{
	string format = "{0, -10} {1, -15}";
	object [] values = { "2" ,"1.32", "ABC", "true", "false", "a", ".", "10.2m"};
	
	Console.WriteLine(format, "Object", "Is Valid Boolean");
	Console.WriteLine(format, "------", "----------------");
	
	foreach(var val in values)
	{
		// C# Extension Method: Object - IsValidBoolean
		Console.WriteLine(format, val, val.IsValidBoolean());
	}
}

View Source
public static partial class Extensions
{
    /// <summary>
    ///     An object extension method that query if '@this' is valid bool.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>true if valid bool, false if not.</returns>
    public static bool IsValidBoolean(this object @this)
    {
        if (@this == null)
        {
            return true;
        }

        bool result;
        return bool.TryParse(@this.ToString(), out result);
    }
}