简体   繁体   English

ReSharper代码检查添加

[英]ReSharper Code Inspection Additions

Is it possible to extend resharper code inspection/annotations to handle cases you know staticly are correct? 是否可以扩展resharper代码检查/注释来处理静态正确的情况?

For example, I have utility function I know satisfy certain conditions, such as: 例如,我知道满足某些条件的效用函数,例如:

    static public bool IsValid(double? d)
    {
        return d != null && IsValid(d.Value);
    }
    static public bool IsValid(double d)
    {
        return !Double.IsNaN(d) && !Double.IsInfinity(d);
    }

So this ensures a nullable has a value, and I'd like the "Possible System.InvalidOperationException" inspection not to fire for something like: 所以这确保了一个可以为空的值有一个值,并且我希望“可能的System.InvalidOperationException”检查不要触发以下内容:

    if (Utils.IsValid(nullableValue))
    {
        DoSomethingWith(nullableValue.Value);
    }

Sure I could suppress the inspection/etc, but is it possible to extend the static typing to indicate that this would actually ensure the value is non-nullable? 当然我可以抑制检查/ etc,但是是否可以扩展静态类型以指示这实际上确保该值不可为空?

(I suppose a related but overly general question is should I be using another static typing check instead of resharper that might handle it, but I won't ask for fear of being overly broad!) (我想一个相关但过于笼统的问题是我应该使用另一个静态打字检查而不是可能处理它的resharper,但我不会要求担心过于宽泛!)

Per Daniel's suggestion, resharper supports a good deal of annotations to assist with inspection. 根据丹尼尔的建议,resharper支持大量注释以协助检查。

Specifically, via the documentation what we're looking for here is something like: 具体来说,通过文档我们在这里寻找的是:

    [ContractAnnotation("d:null => false")]
    static public bool IsValid(double? d)
    {
        return d != null && IsValid(d.Value);
    }

Which does the trick perfectly, and the static check works beautifully. 完美的诀窍,静态检查工作得很漂亮。

Love that resharper! 爱那个resharper!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM