ReSharper 声称我对 serviceProvider 的空检查始终为真,这很奇怪。我认为 serviceProvider 很可能为空。我在这里错了吗,或者这是一个 ReSharper 错误?
public override object ProvideValue(IServiceProvider serviceProvider)
{
switch (Mode)
{
case BindingMode.TwoWay:
throw new InvalidOperationException("Invalid binding mode.");
case BindingMode.OneWayToSource:
throw new InvalidOperationException("Invalid binding mode.");
case BindingMode.OneWay:
case BindingMode.OneTime:
break;
case BindingMode.Default:
if (serviceProvider != null) // Expression is always true?? O.o
{
// Returns something possibly...
}
throw new InvalidOperationException("Invalid binding mode.");
default:
throw new InvalidOperationException("Unexpected binding mode.");
}
return base.ProvideValue(serviceProvider);
}
更新:
我创建了一个控制台应用程序,下面的代码也给出了警告(来自 ReSharper)Possible null assignment to entity marked with 'NotNull' attribute.
var binding = new CustomBinding();
binding.ProvideValue(null);
最佳答案
You can pass null for serviceProvider, but only if this TypeExtension instance was established with an initial true type in the constructor rather than a typeName. Otherwise, this markup extension implementation relies on services based on the passed serviceProvider. It must not be null. The serviceProvider is expected to provide a service for IXamlTypeResolver.
您还可以查看 When can a generic parameter never be null有关这方面的更多信息。
显然,如果我是正确的,这个方法的参数有一个约定 [NotNull],因为一些实现在 null 参数上抛出错误。
关于c# - ReSharper 声称我的表达总是正确的?它是正确的还是错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624521/