3

Is there any fast way to verify null arguments via attributes or something?

Convert this:

public void Method(type arg1,type arg2,type arg3)
{
     if (arg1== null) throw new ArgumentNullException("arg1");
     if (arg2== null) throw new ArgumentNullException("arg2");
     if (arg3== null) throw new ArgumentNullException("arg3");
     //Business Logic
}

Into something like this:

[VerifyNullArgument("arg1","arg2","arg3")]
public void Method(type arg1,type arg2,type arg3)
{
      //Business Logic
}

Ideas? thanks guys.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Fraga
  • 1,361
  • 2
  • 15
  • 47

3 Answers3

4

There are Code Contracts built into .NET 4. That's probably as close as you'll get. There's quite a bit more information at DevLabs if you chose to go this route.

Roman
  • 19,581
  • 6
  • 68
  • 84
  • 2
    Code contracts don't really solve the OP's problem at all, as it still requires just as much typing. But I prefer them because of the potential compile-time checking that can be done. – Josh May 17 '10 at 05:19
  • @Josh Einstein: You're right, in this case it's not much less code. – Roman May 17 '10 at 15:54
3

You're looking for PostSharp.

Chris Lamothe
  • 1,473
  • 13
  • 11
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
-1

not an attribute, but similar idea:

class SomeClass
{
    public static void VerifyNullArgument(params object objects)
    {
        if (objects == null)
        {
            throw new ArgumentNullException("objects");
        }

        for (int index = 0; index < objects.Lenght; index++)
        {
            if (objects[index] == null)
            {
                throw new ArgumentException("Element is null at index " + index,
                    "objects");
            }
        }
    }
}

then in your example method

public void Method(type arg1,type arg2,type arg3)
{
    SomeClass.VerifyNullArgument(arg1, arg2, arg3);
    //Business Logic
}
Steven
  • 166,672
  • 24
  • 332
  • 435
Joel
  • 16,474
  • 17
  • 72
  • 93
  • 1
    This will throw a `NullReferenceException`. You need to take a `Expression>[]`. – SLaks May 17 '10 at 03:35
  • 1
    `if(obj == null) throw new ArgumentNullException(obj.ToString());` This will end up throwing a NullReferenceException when it tries to call obj.ToString(); – Daniel Plaisted May 17 '10 at 03:36
  • haha... silly mistake. :) I obviously wasn't thinking when I wrote that bit. :) But the basic idea is there. – Joel May 17 '10 at 03:40
  • 1
    -1. Fix your "silly mistake" before someone copies and pastes it. – John Saunders May 17 '10 at 03:51
  • It's not just a silly mistake. In order to actually work correctly, this needs to be much more complicated. – SLaks May 17 '10 at 03:53
  • Slaks is right. Look at the improvements I made at Joel's code to get it right. – Steven May 17 '10 at 08:12
  • 1
    @Steven: And it's still wrong. You should throw an `ArgumentNullException` containing the _name_ of the argument that was null. Also, remember that one of the arguments in the middle might be allowed to be null. – SLaks May 17 '10 at 12:33
  • @SLaks: You are completely right. This idea just isn't going to work. I rather stick with CuttingEdge.Conditions ;-) – Steven May 17 '10 at 14:07
  • Thanks for the fix. The code contracts are definitely a better solution. – Joel May 17 '10 at 16:16
  • The problem with this solution is, tha code analisys is still teasing the "verify your params - warning" – Fraga May 17 '10 at 22:52