Fast ArgumentNullException with attributes. Maybe?

Is there a quick way to check for null arguments through 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
}

In something like this:

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

Ideas? Thanks guys.

+3
source share
3 answers

There are Code Contracts built into .NET 4. It's probably as close as you get. Where you have chosen this route, you will find more information in DevLabs .

+4
source

You are looking for PostSharp .

+3
source

, :

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");
            }
        }
    }
}

public void Method(type arg1,type arg2,type arg3)
{
    SomeClass.VerifyNullArgument(arg1, arg2, arg3);
    //Business Logic
}
-1

Source: https://habr.com/ru/post/1745719/


All Articles