[英]Is it possible to force ReSharper to implement ICloneable interface?
[英]Resharper, ICloneable and never null
Resharper抱怨以下代码,说最后一次空检查是多余的,因为'表达式始终为false':
ICloneable data = item as ICloneable;
if (data == null)
throw new InvalidCastException("blah blah, some error message");
object copy = data.Clone();
if (copy == null) // <-- this is where it complains.
return default(T);
怎么知道它永远不会是空的?
ReSharper认为你的对象遵守ICloneable
的合同,其中包括ICloneable
的合同
从检查data
非空的事实以及假设您从ICloneable.Clone()
实现返回相同或兼容类型的对象,ReSharper得出结论,该copy
也是非空的,从而触发警告。
当然,从Clone
返回null
肯定是可能的。 但是,返回null
将是编码错误,因此跳过该空检查是个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.