Monday 23 June 2008

CheckForNull extension method

Debugging is much easier in C# if you remember to check arguments for null.
It's common to see code like this:

public static int Foo(object a, object b, object c)
{
    if (a == null)
    {
        throw new ArgumentNullException("a");
    }
    if (b == null)
    {
        throw new ArgumentNullException("b");
    }
if (c == null)
{
throw new ArgumentNullException("c");
}

    return 0;
}

I think it would be nice if instead we could write:

public static int Foo(object a, object b, object c)
{
    a.CheckForNull();
    b.CheckForNull();
    c.CheckForNull();
    return 0;
}

Well we can with the following [evil] extension method.

[MethodImpl(MethodImplOptions.NoInlining)]
public static void CheckForNull(this object o)
{
    if (o != null) return;
    StackFrame frame = new StackFrame(1, true);
    byte op = frame.GetMethod().GetMethodBody().GetILAsByteArray()[frame.GetILOffset() - 6];
    throw new ArgumentNullException(frame.GetMethod().GetParameters().Length <=
(4 - (frame.GetMethod().IsStatic ? 0 : 1)) ?
        frame.GetMethod().GetParameters()[op - (2 + (frame.GetMethod().IsStatic ? 0 : 1))].Name :
        "An argument was null, but I'm not sure which one.");
}

Is this a nice idea, an utterly horrible idea, or a bit of both?

What do you think?

kick it on DotNetKicks.com