快上执行方法参数的检查方法 [英] Faster way to perform checks on method arguments

查看:89
本文介绍了快上执行方法参数的检查方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是大多只是出于好奇,并可能是一个愚蠢的问题。 :)

This is mostly just out of curiosity, and is potentially a silly question. :)

我有这样一个方法:

public void MyMethod(string arg1, string arg2, int arg3, string arg4, MyClass arg5)
{
    // some magic here
}

的参数都不能为空,并且没有任何字符串参数可以等于的String.Empty

而不是我具有一个大名单:

Instead of me having a big list of:

if(arg1 == string.Empty || arg1 == null)
{
    throw new ArgumentException("issue with arg1");
}

有没有更快的方式只是检查所有的字符串参数?

is there a quicker way to just check all the string arguments?

道歉,如果我的问题不明确。

Apologies if my question isn't clear.

谢谢!

推荐答案

您可以创建或使用的框架要检查你的方法,例如合同 code合同的。

You can create or use frameworks to check contract of your method, e.g. Code Contracts.

您也可以创建各种实用的方法,如 ThrowIfNullOrEmpty 谁将会封装检查参数的逻辑。

Also you can create various utility methods like ThrowIfNullOrEmpty who will encapsulate logic of checking arguments.

这篇关于快上执行方法参数的检查方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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