Search code examples
c#visual-studiotemplatesautomated-refactoringroslynator

change templates for new members


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


Solution

  • 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.