gpt4 book ai didi

c# - ReSharper 好奇号 : "Parameter is only used for precondition check(s)."

转载 作者:IT王子 更新时间:2023-10-29 03:37:33 25 4
gpt4 key购买 nike

为什么 ReSharper 会根据这段代码来评判我?

    private Control GetCorrespondingInputControl(SupportedType supportedType, object settingValue)
{
this.ValidateCorrespondingValueType(supportedType, settingValue);

switch(supportedType)
{
case SupportedType.String:
return new TextBox { Text = (string)settingValue };
case SupportedType.DateTime:
return new MonthPicker { Value = (DateTime)settingValue, ShowUpDown = true };
default:
throw new ArgumentOutOfRangeException(string.Format("The supported type value, {0} has no corresponding user control defined.", supportedType));
}
}

private void ValidateCorrespondingValueType(SupportedType supportedType, object settingValue)
{
Type type;

switch(supportedType)
{
case SupportedType.String:
type = typeof(string);
break;
case SupportedType.DateTime:
type = typeof(DateTime);
break;
default:
throw new ArgumentOutOfRangeException(string.Format("The supported type value, {0} has no corresponding Type defined.", supportedType));
}
string exceptionMessage = string.Format("The specified setting value is not assignable to the supported type, [{0}].", supportedType);
if(settingValue.GetType() != type)
{
throw new InvalidOperationException(exceptionMessage);
}
}

第二个方法 ValidateCorrespondingValueType 的“settingValue”参数被 ReSharper 显示为灰色,并显示以下消息:“参数‘settingValue’仅用于前提条件检查。”

最佳答案

这不是评判,而是试图提供帮助:)

如果 ReSharper 发现参数仅用作抛出异常的检查,它会将其灰显,表明您实际上并未将其用于“实际”工作。这很可能是一个错误 - 为什么要传递一个您不打算使用的参数?它通常表示您已在前提条件下使用它,但后来忘记(或不再需要)在代码的其他地方使用它。

由于该方法是断言方法(也就是说,它所做的只是断言它是有效的),您可以通过使用 ReSharper 的 annotation attributesValidateCorrespondingValueType 标记为断言方法来抑制消息,特别是 [AssertionMethod] 属性:

[AssertionMethod]
private void ValidateCorrespondingValueType(SupportedType supportedType, object settingValue)
{
// …
}

关于c# - ReSharper 好奇号 : "Parameter is only used for precondition check(s).",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27010269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com