C#: How to Implement and use a NotNull and CanBeNull attribute

后端 未结 4 1485
执笔经年
执笔经年 2020-12-08 07:15

I want to let programmers and myself know that a method does not want null and if you do send null to it anyways, the result will not be pretty.

4条回答
  •  有刺的猬
    2020-12-08 07:41

    This can be done either with AOP, whereby an Advice verifies at run-time whether a method parameter is null and whether nulls are allowed. See PostSharp and Spring.NET for AOP.

    As for ReSharper, see Annotated Framework:

    We have analyzed a great share of .NET Framework Class Library, as well as NUnit Framework, and annotated it through external XML files, using a set of custom attributes from the JetBrains.Annotations namespace, specifically:

    • StringFormatMethodAttribute (for methods that take format strings as parameters)
    • InvokerParameterNameAttribute (for methods with string literal arguments that should match one of caller parameters)
    • AssertionMethodAttribute (for assertion methods)
    • AssertionConditionAttribute (for condition parameters of assertion methods)
    • TerminatesProgramAttribute (for methods that terminate control flow)
    • CanBeNullAttribute (for values that can be null)
    • NotNullAttribute (for values that can not be null)

提交回复
热议问题