Cor*_*ker 92 c# resharper preconditions
为什么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);
}
}
Run Code Online (Sandbox Code Playgroud)
第二种方法ValidateCorrespondingValueType的"settingValue"参数显示为灰色,ReSharper使用以下消息:"参数'settingValue'仅用于前置条件检查."
cit*_*att 96
这不是判断,它试图帮助:)
如果ReSharper看到一个参数仅用作检查以抛出异常,它会将其变灰,表明您实际上并未将其用于"真实"工作.这很可能是一个错误 - 为什么要传递一个你不会使用的参数?它通常表示您已在先决条件中使用它,但后来忘记(或不再需要)在代码中的其他位置使用它.
由于该方法是一个断言方法(也就是说,它所做的只是断言它是有效的),你可以通过ValidateCorrespondingValueType使用ReSharper的注释属性,特别是[AssertionMethod]属性标记为断言方法来抑制消息:
[AssertionMethod]
private void ValidateCorrespondingValueType(SupportedType supportedType, object settingValue)
{
// …
}
Run Code Online (Sandbox Code Playgroud)
Hol*_*olf 17
有趣的是,如果你使用nameofC#6中的新功能,ReSharper会退缩:
static void CheckForNullParameters(IExecutor executor, ILogger logger)
{
if (executor == null)
{
throw new ArgumentNullException(nameof(executor));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
}
Run Code Online (Sandbox Code Playgroud)
我首选的解决这个问题是让ReSharper的认为参数被使用.这比使用属性的优势,比如UsedImplicitly,因为如果你永远 不停止使用该参数,ReSharper的将开始再次警告你.如果使用属性,resharper也不会捕获未来的真实警告.
使resharper认为使用参数的一种简单方法是用throw方法替换.而不是......
if(myPreconditionParam == wrong)
throw new Exception(...);
Run Code Online (Sandbox Code Playgroud)
...你写:
if(myPreconditionParam == wrong)
new Exception(...).ThrowPreconditionViolation();
Run Code Online (Sandbox Code Playgroud)
对于未来的程序员来说,这很好地自我记录,并且resharper退出了抱怨.
ThrowPreconditionViolation的实现是微不足道的:
public static class WorkAroundResharperBugs
{
//NOT [Pure] so resharper shuts up; the aim of this method is to make resharper
//shut up about "Parameter 'Foobaar' is used only for precondition checks"
//optionally: [DebuggerHidden]
public static void ThrowPreconditionViolation(this Exception e)
{
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
Exception的一个扩展方法是命名空间污染,但是它被公平地包含了.
小智 5
以下修复了该问题(在ReSharper 2016.1.1,VS2015中),但我不确定它是否解决了"正确"的问题.无论如何,它显示了ReSharper关于这个主题的机制的模糊性:
这会产生警告:
private void CheckForNull(object obj)
{
if (ReferenceEquals(obj, null))
{
throw new Exception();
}
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
private void CheckForNull(object obj)
{
if (!ReferenceEquals(obj, null))
{
return;
}
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,等效代码(由ReSharper:D完成的反演)给出了不同的结果.似乎模式匹配根本没有拿起第二个版本.
| 归档时间: |
|
| 查看次数: |
14900 次 |
| 最近记录: |