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

╄→尐↘猪︶ㄣ 提交于 2019-11-29 10:49:06

问题


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.


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/2875858/how-do-i-tell-resharper-that-an-attribute-means-that-a-method-is-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!