How do I tell ReSharper that an attribute means that a method is used?
Asked Answered
E

3

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.

Exceed answered 20/5, 2010 at 16:33 Comment(1)
Oddly, on another computer, R# doesn't warn about the 'unused' methods. Is there a configuration setting I could have changed?Exceed
E
33

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.

Elaina answered 21/5, 2010 at 6:21 Comment(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.Elaina
@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?Kellsie
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.Zounds
Is there something like MeansCanBeNull? I have a custom attribute, and I want R# to treat it as its CanBeNull attribute.Dariadarian
M
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!

Mathewmathews answered 20/6, 2012 at 17:5 Comment(0)
D
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
    }
Depressant answered 3/12, 2020 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.