简体   繁体   中英

How can I configure ReSharper to accept [Required] (from PostSharp) as equivalent to [NotNull] (or vice versa)?

I'm using PostSharp and ReSharper together in a variety of projects, specifically, making use of both PostSharp's code contract enforcement, and of ReSharper's annotations in the interest of better code.

Trouble is, when it comes to nullability, I end up with lots of parameter, etc., declarations that look like this:

public void Foo ([Required] [JetBrains.Annotations.NotNull] object bar)

...in order to both enforce with the former and annotate with the latter that bar cannot be null.

Is there a way in which I can configure ReSharper to treat the presence of the [Required] attribute as equivalent to [NotNull] for annotation purposes, or some other method (I'm not particular as to how) to avoid having to repeat myself like this for every not-null parameter, etc.?

As described on this documentation page , ReSharper can actually recognize custom annotations even when they are defined in the namespace other than "JetBrains.Annotations". So you can define your own custom annotations and turn some of them into aspect providers that apply a code contract to the target element.

First, open "ReSharper Options" | "Code Annotations" and click "Copy C# implementation to clipboard". Also, uncheck "internal" to reuse annotations across projects.

Then create a new code file and paste annotations. You can rename the namespace (eg MyAnnotations ). If you choose to use custom namespace, you need to open "ReSharper Options" again and select the check-box next to your custom namespace in "Custom namespaces should be marked in the list below".

Now you can find NotNullAttribute and change its source code in the following way:

public sealed class NotNullAttribute : LocationLevelAspect, IAspectProvider
{
    public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
    {
        var requiredConstruction = new ObjectConstruction( typeof( PostSharp.Patterns.Contracts.RequiredAttribute ) );
        yield return new AspectInstance( targetElement, requiredConstruction, null );
    }
}

You can apply this custom attribute in your code and it will work as both PostSharp aspect and ReSharper annotation.

public void Foo ([MyAnnotations.NotNull] object bar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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