Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 740
Roger Lipscombe Avatar asked May 20 '10 16:05

Roger Lipscombe


2 Answers

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!

like image 42
rythos42 Avatar answered Oct 03 '22 11:10

rythos42


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.

like image 125
Ilya Ryzhenkov Avatar answered Oct 03 '22 13:10

Ilya Ryzhenkov