-2

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?

qqqqqqq
  • 1,831
  • 1
  • 18
  • 48
  • I'm sure there's an editorconfig property you can use. That's likely the only way as I doubt there's anything "built-in" – pinkfloydx33 May 16 '21 at 11:41
  • See this, it refers to VS but assuming vscode respects editorconfig (it should!) then it doesn't matter that he's talking about VS https://www.tabsoverspaces.com/233829-removing-explicit-default-access-modifiers-in-visual-studio-using-editorconfig should at least point in right direction anyways – pinkfloydx33 May 16 '21 at 11:42
  • @pinkfloydx33, I was not able to achieve what I am asking for in the question with the help of the `editorconfig`. – qqqqqqq Jun 06 '21 at 09:41

1 Answers1

-1

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.

Tim
  • 2,587
  • 13
  • 18
  • `While this wouldn't affect how the methods are initially generated` if it would not, then why do you think that it may be an appropriate answer? – qqqqqqq May 27 '21 at 15:23
  • Your problem appears to be compliance with corporate standards. This solution could actually help take enforcement to the next level (by failing a build) if a developer chose to use private anyway even if the templates were fixed. It is admittedly more of a workaround than a fix though if you want to get technical. – Tim May 27 '21 at 15:26
  • It is not a workaround. With what you suggest I do not get what I am asking for in my question. – qqqqqqq May 27 '21 at 15:27
  • I don't think what you're asking for is possible today. The access modifier is set in Roslyn [here](https://github.com/dotnet/roslyn/blob/main/src/Features/Core/Portable/ImplementInterface/AbstractImplementInterfaceService.CodeAction_Method.cs), which is called [here](https://github.com/dotnet/roslyn/blob/main/src/Features/Core/Portable/ImplementInterface/AbstractImplementInterfaceService.CodeAction.cs#L399) – Tim May 27 '21 at 17:10
  • The code that determines accessibility looks like this: `var accessibility = member.Name == memberName || generateAbstractly ? Accessibility.Public : Accessibility.Private;` (sorry about the formatting, nothing is working to fix it) If you want to be able to do what you're asking, a change is needed first in Roslyn, then possibly OmniSharp after that – Tim May 27 '21 at 17:12
  • Sorry you didn't like the answer, but I just dug through how many layers to find the exact source of your complaint and this is your response? Being rude to people who are going out of their way to dig deep into your problem isn't a great way to make friends. – Tim May 27 '21 at 17:19
  • I gave you an upvote for your efforts. I am not trying to make friends. Making two conradictory statements is not helpful at all, even if you spent a lot of efforts to come with these two statements, they are still useless. In the end of the day I say thank you. – qqqqqqq May 28 '21 at 17:25