Object - ToNullableGuidOrDefault

An object extension method that converts this object to a nullable unique identifier or default.

Try it

public static void Main()
{
	object val1 = "ca761232ed4211cebacd00aa0057b223";
    object val2 = "{0xCA761232, 0xED42, 0x11CE, {0xBA, 0xCD, 0x00, 0xAA, 0x00, 0x57, 0xB2, 0x23}}";
	object thisNull = null;
	object thisInvalid = "A100";
	
	string format = "{0, -100} {1, -15}";
	
	Console.WriteLine(format, "Object", "ToNullableGuidOrDefault");
	Console.WriteLine(format, "------", "-----------------------");
	
	// C# Extension Method: Object - ToNullableGuidOrDefault
	Console.WriteLine(format, val1, val1.ToNullableGuidOrDefault());
	Console.WriteLine(format, val2, val2.ToNullableGuidOrDefault());
	Console.WriteLine(format, "null", thisNull.ToNullableGuidOrDefault());
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableGuidOrDefault());
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableGuidOrDefault(Guid.NewGuid()));
	Console.WriteLine(format, thisInvalid, thisInvalid.ToNullableGuidOrDefault(() => Guid.NewGuid()));
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     An object extension method that converts this object to a nullable unique identifier or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>The given data converted to a Guid?</returns>
    public static Guid? ToNullableGuidOrDefault(this object @this)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return new Guid(@this.ToString());
        }
        catch (Exception)
        {
            return Guid.Empty;
        }
    }

    /// <summary>
    ///     An object extension method that converts this object to a nullable unique identifier or default.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="defaultValue">The default value.</param>
    /// <returns>The given data converted to a Guid?</returns>
    public static Guid? ToNullableGuidOrDefault(this object @this, Guid? defaultValue)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return new Guid(@this.ToString());
        }
        catch (Exception)
        {
            return defaultValue;
        }
    }

    /// <summary>
    ///     An object extension method that converts this object to a nullable unique identifier 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 Guid?</returns>
    public static Guid? ToNullableGuidOrDefault(this object @this, Func<Guid?> defaultValueFactory)
    {
        try
        {
            if (@this == null || @this == DBNull.Value)
            {
                return null;
            }

            return new Guid(@this.ToString());
        }
        catch (Exception)
        {
            return defaultValueFactory();
        }
    }
}