views:

88

answers:

4

Hi.

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.

+3  A: 

You're looking for PostSharp.

SLaks
+4  A: 

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.

R0MANARMY
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 Einstein
@Josh Einstein: You're right, in this case it's not much less code.
R0MANARMY
A: 

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
}
Joel
This will throw a `NullReferenceException`. You need to take a `Expression<Func<object>>[]`.
SLaks
`if(obj == null) throw new ArgumentNullException(obj.ToString());` This will end up throwing a NullReferenceException when it tries to call obj.ToString();
Daniel Plaisted
haha... silly mistake. :) I obviously wasn't thinking when I wrote that bit. :) But the basic idea is there.
Joel
-1. Fix your "silly mistake" before someone copies and pastes it.
John Saunders
It's not just a silly mistake. In order to actually work correctly, this needs to be much more complicated.
SLaks
Slaks is right. Look at the improvements I made at Joel's code to get it right.
Steven
@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
@SLaks: You are completely right. This idea just isn't going to work. I rather stick with CuttingEdge.Conditions ;-)
Steven
Thanks for the fix. The code contracts are definitely a better solution.
Joel
The problem with this solution is, tha code analisys is still teasing the "verify your params - warning"
Fraga
A: 

When you have the Ultimate edition of Visual Studio 2010, I think Code Contracts is the way to go. Otherwise you might take a look at the CutingEdge.Conditions library (shameless plug). It has a fluent interface. There are people that prefer it over Code Contracts :-). Here is an example:

public void Method(type arg1, type arg2, params type[] col)
{
    Condition.Requires(arg1, "arg1").IsNotNull();
    Condition.Requires(arg2, "arg2").IsNotNull();
    Condition.Requires(col, "col")
        .IsNotNull()
        .IsNotEmpty()
        .DoesNotContain(null);

    //Business Logic
}
Steven
Code Contracts are not tied to Visual Studio Ultimate. Only the static checker is tied to the VS Premium.
Peli
You are correct. However, without the static check it is pretty useless.
Steven