如何检查许多参数的空字符串或空字符串?- C#

本文关键字:字符串 参数 何检查 检查 许多 | 更新日期: 2023-09-27 18:37:27

我有以下方法,我需要检查参数是空还是空。

    public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
    {
        _Where = " WHERE " + field + " " + operat + " @" + field + "1 " + andOr + " " + field2 + " " + operat2 + " @" + field2 + "2 ";
        _Params.Add(field + "1", value);
        _Params.Add(field2 + "2", value2);
        return this;
    }

我找到了字符串。IsNull或WhiteSpace方法,但这需要这么多代码:

                   if (string.IsNullOrWhiteSpace(field))
            throw new ArgumentException("field Cannot be null or be empty");
        if (string.IsNullOrWhiteSpace(operat))
            throw new ArgumentException("operat Cannot be null or be empty");
        if (string.IsNullOrWhiteSpace(value))
            throw new ArgumentException("value Cannot be null or be empty");
        if (string.IsNullOrWhiteSpace(andOr))
            throw new ArgumentException("andOr Cannot be null or be empty");
        if (string.IsNullOrWhiteSpace(field2))
            throw new ArgumentException("field2 Cannot be null or be empty");
        if (string.IsNullOrWhiteSpace(operat2))
            throw new ArgumentException("operat2 Cannot be null or be empty");
        if (string.IsNullOrWhiteSpace(value2))
            throw new ArgumentException("value2 Cannot be null or be empty");

有什么方法可以缩短这个吗?

此外,我尝试为此任务创建自定义方法,但是它在自定义方法而不是 Where() 方法中抛出异常,这使得调试变得棘手。

如何检查许多参数的空字符串或空字符串?- C#

您可以逐个检查值或创建中间函数来执行此操作。

或者,我的建议是:您可以将所有输入放在一个数组中,并使用 LINQ Any 一次检查所有输入:

public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
{
    string[] inputs = {field, operat, value, andOr, field2, operat2, value2}
    if (inputs.Any(x => string.IsNullOrWhiteSpace(x))){
        //throw exception
    }
    //continue with your method, all inputs are OK
}

我能建议的是:

private string _nullChecker(string _value){
   if (string.IsNullOrWhiteSpace(_value))
            throw new ArgumentException(_value + "Cannot be null or be   empty");
   return _value;
}

然后,在您的 Where 字符串语句中

_Where = " WHERE " + _nullChecker(field) + " " + __nullChecker(operat) + " @" + _nullChecker(field) + "1 " + _nullChecker(andOr) + " " + _nullChecker(field2) + " " + _nullChecker(operat2) + " @" + _nullChecker(field2) + "2 ";

不过不确定。尚未使用实际代码进行检查。:)希望这有帮助

你可以

这样做:

int? GetLength(string s) {
    return s == "" ? -1 : s?.Length;
}
// s1, s2 and so on are your parameters
int? lengthSum = GetLength(s1) + GetLength(s2); // and so on
int wholeLength = (s1 + s2).Length; // and so on
if(lengthSum == wholeLength) {
    // No parameter is null or empty
}

首先,您可以使用简单的库来执行参数验证。看看这个叫做参数验证器,它有方便的功能,可以将你的整体代码减少一半。

下面是如何使用参数验证器库执行此操作的示例:

public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
{
    var inputs = new string[] {field, operat, value, andOr, field2, operat2, value2};
    foreach(var input in inputs)
    {
        Throw.IfNullOrEmpty(input, nameof(input)));
    }
}    

我想我找到了你正在做的事情的"分支"。下面要检查的代码较少,错误在函数中抛出。

亚当·斯托尔(Adam Storr)有一篇关于参数检查的好文章

对于懒惰的人,您会发现使用扩展方法,允许在try/catch内的一行中检查参数。

它允许独立检查所有参数,并提供自定义反馈

public returnType MyFunction(string arg1, string arg2, int arg3)
{
    try
    {
        // Check that the arguments are not null or empty
        _ = arg1.Valid() ?? throw new ArgumentNullException("Argument 1 not valid");
        _ = arg2.Valid() ?? throw new ArgumentNullException("The argument 2 must be provided");
        _ = arg3.Valid() ?? throw new ArgumentNullException("I need arg3 to work");
    }
    catch(ArgumentNullException)
    {
    //Treat the error
    }

并实施.Valid() 方法发生在您想要的另一个文件中(在本例中存储在 TypeExtension 文件中):

 public static class TypeExtensions
    {
        /// <summary>
        /// String method extension that return null if the string is null or empty.<br/>
        /// Otherwise return the value itself
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Valid(this string value)
        {
            return (!string.IsNullOrWhiteSpace(value)) ? value : null;
        }
        /// <summary>
        /// int method extension that return null if the int is null or 0 <br/>
        /// Otherwise return the value itself
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Valid(this int? value)
        {
            if (value is null or 0)
                return null;
            else
                return value;
        }
    }

因此,参数通过其类型继承了方法 Valid() 的方法。"空合并运算符"(??)表示"如果值为空,则给出下一步"丢弃"_"意味着无论返回什么,它都不会存储在内存中。

因此,通过两者的关联,您可以检查参数,如果检查返回 null,则抛出异常。

我同意这是为了扭曲的头脑,但到目前为止我没有找到"更干净"的方法。也许用装饰器?

我希望它可能会有所帮助!