Resharper:如何使用另一个属性隐式应用[NotNull]或[CanBeNull]注释?

Jef*_*hao 6 c# resharper static-analysis

在我们的代码中,我们有自己的注入框架来启用依赖注入,例如:

public class MyClass {
    [Component("ComponentName1")] // means [NotNull]
    public MyType NotNullComponent{ get; set; }

    [OptionalComponent("ComponentName2")] // means [CanBeNull]
    public MyType CanBeNullComponent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我们不希望再次添加[CanBeNull][NotNull]在任何地方添加,因为它具有[Component][OptionalComponent]属性重复的语义.

是否有可能让Resharper知道自定义属性的语义来帮助进行静态分析?

rol*_*lls 0

现在,您可以通过在“代码检查”->“设置”中启用“隐式”,在所有内容上隐式使用 [NotNull]

除非您指定 [CanBeNull],否则所有内容都将被假定不允许为 null。

在此输入图像描述