Tool for automatically testing method parameters and throwing ArgumentNullException for .net / VS?

When I write code, I have this strange habit of running my methods with code that just throws exceptions like this

public WhateverType MethodName(CrazyObjectType crazyObject, string para2){
    if(crazyObject == null){
        throw new ArgumentNullException("crazyObject");
    }
    if(param2 == null){
        throw new ArgumentNullException("param2");
    }
    if(para2.Lenght > 32){
        throw new ArgumentOutOfBoundsException("para2");
    }

    ...
}

Consider that some methods have 6 parameters, and not one of them is allowed to be zero, and some of them are strings that may not be empty or longer than some value. You can imagine that before I get to the actual logic of the method, I will write quite some code + my class will be harder to read.

Is there any tool / plugin VS / Resharper plugin / annotation / code snippet for me not to write all this duplicate code?

An ideal solution would be some declarative annotation, for example:

[NotNull("crazyObject, param2")] [StringLenght("para2", 0, 32)]
public WhateverType MethodName(CrazyObjectType crazyObject, string para2){
    ...
}
  • VS/Resharper, , , /, ?

, , GUI , , -, , , .

- ?


. , , , .


: " 6 " - - , . ", , 3-4 ArgumentException s".

EDIT2: . , , "" , , , "" .NET, .NET.

+1
2

, , , , .

, . , .

, , . .

+5

. - , , .

+2

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


All Articles