29

我正在玩SpecFlow,而 ReSharper 认为我的步骤定义未使用(我猜是因为它们是通过反射使用的):

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

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

    // ...
}

我如何告诉 ReSharper 实际使用了带有[Given], [When],[Then]属性(等)的方法?我不想使用// ReSharper disable UnusedMember.Global评论。

我还可以用 . 标记每个方法(或整个类)[JetBrains.Annotations.UsedImplicitly]。我也不是特别想这样做。

4

3 回答 3

30

您需要使用 JetBrains Annotations,并使用MeansImplicitUseAttribute. 您可以直接引用,也可以将注释源代码(来自ReSharper / Options / Code Inspection / Code AnnotationsJetBrains.Annotations.dll )复制到您的解决方案中。

如果您需要注释某些不属于您的外部程序集,则需要在以下文件夹中创建一个外部注释文件 (xml) %ReSharperInstallDir%\Bin\ExternalAnnotations:. 例子很多,你可以复制一些。

如果您命名,外部注释文件也可以与 DLL 位于同一路径中DllNameWithoutExtension.ExternalAnnotations.xml

于 2010-05-21T06:21:03.397 回答
10

有很多例子,但我想更明确一点,以防你不想追踪一个例子。:)

在 %ReSharperInstallDir%\Bin\ExternalAnnotations 中创建一个具有属性程序集 (.xml) 名称的文件。例如,我制作了 Microsoft.VisualStudio.QualityTools.CodedUITestFramework.xml 并将这个 XML 放入其中:

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

重新启动 VS,您就可以上路了!

于 2012-06-20T17:05:36.497 回答
0

这些答案有所帮助,但值得注意的是,如果您要装饰您将要使用该UsedImplicitly属性的界面

    [UsedImplicitly]
    public interface ISomeInterface
    {
        //... stuff
    }
于 2020-12-03T14:46:39.507 回答