Search code examples
c#methodsnunitwarnings

Is it possible to mark a method as explicitly used in C#?


I have a method that is accessed by NUnit only with its name via reflection, and I can't use nameof() because this is a TestCaseSource accessing test data in a static method in a derived class (the input data and [SetUp] are different for each derived class, but the execution of the test is the same). This method doesn't need to be public/protected for NUnit to work, so for minimizing unwanted access I would like to set it to private, however, if I set it as private then I get a warning that the method is unused.

Is there a way to mark a method as explicitly used? Perhaps an attribute, or a preprocessor directive, that can tell the analyzer that this method is used?


Solution

  • No, but you can suppress the warning by surrounding the method declaration with a preprocessor directive. I'm assuming the warning you are seeing is CS0067. Example:

    #pragma warning disable CS0067
    private void MyMethod()
    #pragma warning restore CS0067
    {
       // Method body
    }