Search code examples
c#visual-studio-codeprivateaccess-modifiers

How can I prevent the `private` access modifier from being added in VS Code for C# when generating members?


How can I prevent the private access modifier from being added in VS Code for C# when generating members?

E.g. I have the error:

The name 'foo' does not exist in the current context [Assembly-CSharp]csharp(CS0103)

enter image description here

I focus the caret at the foo(); with the help of the cursor, press Ctrl+. and select the

Generate method 'MyClass.foo'

enter image description here

Here is the method which gets generated:

private void foo()
{
    throw new NotImplementedException();
}

As you can see I am getting the private modifier being added.

Here is what I want to be generated instead of the one I showed above:

void foo()
{
    throw new NotImplementedException();
}

I tried searching for the private, accessibility settings in VS Code but found nothing. Given that VS Code is quite configurable, I am expecting to be able to set up this config as well. If you know how to do that, could you share it with me, please?


Solution

  • I don't believe that what you are asking for is possible today. The access modifier is set in Roslyn here, which is called from here. As you can see in the code, this setting is not exposed to any layer above, and you're going to get either public or private:

    var accessibility = member.Name == memberName || generateAbstractly
          ? Accessibility.Public
          : Accessibility.Private;
    

    Given that it is not possible today, you could file an issue with Roslyn and/or OmniSharp to request this functionality, or you could look at building a custom Roslyn Analyzer.

    There is a tutorial available that includes information on how to provide a code fix for quickly handling violations.