用于自动测试方法参数并为 .net/VS 抛出 ArgumentNullException 的工具? [英] A tool for automatically testing parameters of methods and throwing ArgumentNullException for .net/VS?

查看:15
本文介绍了用于自动测试方法参数并为 .net/VS 抛出 ArgumentNullException 的工具?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编写代码时,我有一个奇怪的习惯,即使用只会抛出这样的异常的代码来启动我的方法

When I write code I have this strange habit to start 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");
    }

    ...
}

考虑一些方法有 6 个参数,其中不允许为 null,其中一些是字符串,可能不为空或比某个值长.你可以想象,在我开始真正的方法逻辑之前,我写了很多代码 + 我的类会更难阅读.

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

是否有任何工具/VS 插件/Resharper 插件/注释/代码片段让我不必编写所有这些重复的代码?

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

理想的解决方案是一些声明式注释,例如:

Ideal solution would be some declarative annotation something like:

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

  • 一些 VS/Resharper 插件允许折射这些参数,所以当我重命名/折射一些参数注释时会自动同步?
  • 我的理想形象更进一步,我想我可以有 GUI 来自动化过程,这样我就可以选择方法单击某处,检查一些不应该为空的参数的复选框,并且将为我生成注释.

    And my ideal image goes even further and I imagine that I can have GUI to automatize process so that I select method click somewhere, check some check boxes for parameters that should not be null and annotation will be generated for me.

    这样的东西存在吗?

    注意:我想我总是可以创建代码截图来生成代码,但声明式方法和 GUI 生成器会更好.

    说考虑一些方法有 6 个参数"只是一个例子 - 我认为我没有很多这样的方法.相反,我应该写考虑到您有很多方法会抛出一些 3-4 个 ArgumentException s".

    Saying "Consider some methods have 6 parameters" is just an example - I don't think I have many if any such methods. Instead I should have written "consider you have a lot of methods that throw some 3-4 ArgumentExceptions".

    糟糕的术语.我意识到我在这个问题中到处都称之为注释",如果我从我的 .NET 类中正确地记住了它,我的评论在 .NET 中可能应该被称为属性".

    Bad terminology. I realized that what I call "annotation" everywhere in this question and my comments should be probably called "attributes" in .NET if I remember it correctly from my .NET classes.

    推荐答案

    虽然它不是严格的声明性的,但我认为你应该看看 代码契约.

    While it's not strictly declarative, I think you should probably look at Code Contracts.

    对于使空参数测试更便宜的奇怪且不推荐的方法,请查看 这篇博文.这涵盖了空参数检查 - 而不是其他类型的有效性.

    For a bizarre and not-really-recommended way of making null argument testing cheaper, have a look at this blog post. That only covers null argument checking though - not other kinds of validity.

    顺便说一下,方法的第一部分只是执行参数验证并没有什么奇怪的.完全合理.

    By the way, there's nothing strange about the first part of a method just performing argument validation. It's entirely reasonable.

    这篇关于用于自动测试方法参数并为 .net/VS 抛出 ArgumentNullException 的工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆