2

when I do refactoring in visual studio (2019) and I say "generate field", it per default always adds the "private" keyword before the new field. I don't want this. How can I modify this behavior?

I am not event sure if this functionality is provided by visual studio itself or by roslinator.

private int foo; // no!
int foo; // yes!

enter image description here

codymanix
  • 28,510
  • 21
  • 92
  • 151

1 Answers1

1

If you go in the Tools|Code Snippets Manager... menu:

Code Snippets Manager... menu

It will open the code snippets manager.

  • Select CSharp in the language drop down.
  • In the tree go to Refactoring|Generate field

code snippets manager

It will give you the location of the snippet, on my machine (with French locale) it's:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC#\Snippets\1033\Refactoring\GenerateField.snippet

You can then edit the file to change the accessibility to the desired value.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Generate Field</Title>
            <Description>Snippet for the field created by the 'Generate Field' refactoring</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Refactoring</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="true">
                    <ID>accessibility</ID>
                    <!-- put public here, actually on my machine it is already public -->
                    <Default>public</Default>
                </Literal>
                <Literal Editable="true">
                    <ID>modifiers</ID>
                    <Default></Default>
                </Literal>
                <Literal Editable="true">
                    <ID>type</ID>
                    <Default>type</Default>
                </Literal>
                <Literal Editable="true">
                    <ID>signature</ID>
                    <Default>signature</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[$end$$accessibility$ $modifiers$ $type$ $signature$;]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

But you can also create your own snippet based on this one, call it 'Generate Public Field' and register it as described here.

Edit:

I did not find anything about this in Roslynator sources. It definitly looks like a VS quick action. There was a bug in Roslyn/VS about something similar here and was related to a bug that impact many quick action. This bug has been fixed. Looks like you just have to configure your editor:

Add the following to an .editorconfig file at the root of your project: dotnet_style_require_accessibility_modifiers = omit_if_default:suggestion

See the options for dotnet_style_require_accessibility_modifiers here.

Orace
  • 7,822
  • 30
  • 45
  • yes. also since the template says public for default it is obvious that it is the wrong one. but thanks anyway for trying to help. I added an image to show what iam trying to do. – codymanix Mar 11 '21 at 13:46
  • @codymanix Mayby if it's roslynator (I don't use that tool), take a look here https://github.com/JosefPihrt/Roslynator/blob/master/docs/HowToConfigureRefactorings.md – Orace Mar 11 '21 at 14:09
  • it seems the only options that can be configured in roslynator are disabling analyzers/refactorings – codymanix Mar 11 '21 at 18:57
  • @codymanix, I have edited my response with new information. – Orace Mar 11 '21 at 22:46
  • does not help either. – codymanix Mar 11 '21 at 23:42