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

后端 未结 4 1486
执笔经年
执笔经年 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:30

    As pointed by Anton Gogolev, attributes can be created using PostSharp.(note that CodeContract is using static method calls inside body of method)

    UPDATE Feb 2013: new 3.0 release of PostSharp (currently in Beta) will support Validating parameters, fields and properties

    1) Article validate-parameters-using-attributes has implementation of

    public class NotEmpty : ParameterAttribute

    public class NotNull : ParameterAttribute

    [AttributeUsage(AttributeTargets.Parameter)]

    public abstract class ParameterAttribute : Attribute

    {

    public abstract void CheckParameter(ParameterInfo parameter, object value); 
    

    }

    It also Required a method attribute with a method boundary aspect to process the parameter attributes.

    2) In the comment to the article there are links to very similar implementation for NonNull/NonEmpty    

    [return: NonNull] public SomeObject SomeMethod([NonNull] AnotherObject param1)      

    The source code is located In google code Torch/DesignByContract

    3) another more complicate example is described in http://badecho.com/2011/11/validating-method-parameters-with-postsharp/       

    0 讨论(0)
  • 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)
    0 讨论(0)
  • 2020-12-08 07:42

    In the mid-term, "code contracts" (in 4.0) will be a better answer to this. They are available now (with academic or commercial licences), but will be more integrated in VS2010. This can provide both static analysis and runtime support.

    (edit) example:

    Contract.RequiresAlways( x != null );
    

    Simple as that... the code contracts engine works at the IL level, so it can analyse that and throw warnings/errors from calling code during build, or at runtime. For backwards compatibility, if you have existing validation code, you can just tell it where the sanity checking ends, and it'll do the rest:

    if ( x == null ) throw new ArgumentNullException("x");
    Contract.EndContractBlock();
    
    0 讨论(0)
  • 2020-12-08 07:55

    These annotations are for ReSharper, and are copied from the JetBrains.Annotations namespace. A framework can put them in their own namespace, however, ReSharper will NOT pick up these annotations automatically - you need to tell ReSharper to use the custom namespace in the options dialog. Once you've selected the new namespace, ReSharper's analysis will pick up the attributes and give you highlights and warnings.

    0 讨论(0)
提交回复
热议问题