31

I'm playing with SpecFlow, and ReSharper thinks that my step definitions are unused (I guess because they're used via reflection):

[Binding]
public class StepDefinitions
{
    // ...

    [When(@"I press add")]
    public void WhenIPressAdd()   // R# thinks this is unused
    {
        _calculator.PressAdd();
    }

    // ...
}

How can I tell ReSharper that methods with [Given], [When], [Then] attributes (etc.) are actually used? I don't want to use // ReSharper disable UnusedMember.Global comments.

I could also mark each method (or the whole class) with [JetBrains.Annotations.UsedImplicitly]. I don't particularly want to do that either.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380

3 Answers3

32

You need to use JetBrains Annotations, and mark the attribute with an MeansImplicitUseAttribute. You can either reference JetBrains.Annotations.dll directly, or you can copy the annotations source code (from ReSharper / Options / Code Inspection / Code Annotations) into your solution.

If you need to annotate some external assembly you don't own, you need to create an External Annotation file (xml) in the following folder: %ReSharperInstallDir%\Bin\ExternalAnnotations. There are plenty of examples, you can just copy some.

The external annotations file can also be in the same path as the DLL if you name it DllNameWithoutExtension.ExternalAnnotations.xml.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
Ilya Ryzhenkov
  • 11,782
  • 1
  • 40
  • 50
  • 4
    Yes, you can. You need to create an External Annotation file (xml) in the following folder: %ReSharperInstallDir%\Bin\ExternalAnnotations. There are plenty of examples, you can just copy some. – Ilya Ryzhenkov Jun 16 '10 at 10:01
  • 1
    @IlyaRyzhenkov is there any way to deploy external annotations with the library (I do not want to inline or reference the JetBrains.Annotations.dll assembly) with no forcing user to place these annotations into %ReSharperInstallDir%\Bin\ExternalAnnotations folder? – hazzik Mar 30 '12 at 20:07
  • 5
    The external annotations file can also be in the same path as the DLL if you name it `DllNameWithoutExtension.ExternalAnnotations.xml` - works perfectly for my library. I have this file as part of my C# project and set it to be copied to the output directory on build. – Lucero May 16 '12 at 16:34
  • Is there something like `MeansCanBeNull`? I have a custom attribute, and I want R# to treat it as its `CanBeNull` attribute. – vorou May 16 '16 at 07:53
10

There are plenty of examples, but I wanted to be a little more explicit in case you don't want to track down an example. :)

Create a file with the name of the attribute's assembly (.xml) in %ReSharperInstallDir%\Bin\ExternalAnnotations. For example, I made Microsoft.VisualStudio.QualityTools.CodedUITestFramework.xml and put this XML inside it:

<assembly name="Microsoft.VisualStudio.QualityTools.CodedUITestFramework">
  <member name="T:Microsoft.VisualStudio.TestTools.UITesting.CodedUITestAttribute">
    <attribute ctor="M:JetBrains.Annotations.MeansImplicitUseAttribute.#ctor" />
  </member>
</assembly>

Restart VS and you're on your way!

rythos42
  • 1,217
  • 2
  • 11
  • 27
0

these answers have helped but note worthy if you are looking to decorate an interface you will want to use the UsedImplicitly attribute

    [UsedImplicitly]
    public interface ISomeInterface
    {
        //... stuff
    }
workabyte
  • 3,496
  • 2
  • 27
  • 35