VS 2017:如何修改‘Convert to full property’模板去除下划线并换行?
Got it, let's walk through exactly how to tweak that refactoring template to get private fields without underscores and multi-line full properties—this is a common adjustment I've made a few times for teams that prefer that coding style!
Step 1: Locate the Refactoring Template File
First, navigate to the Visual Studio 2017 refactoring templates folder. Replace <Edition> with your VS version (Community, Professional, or Enterprise):
C:\Program Files (x86)\Microsoft Visual Studio\2017\<Edition>\Common7\IDE\Templates\Refactoring\CSharp
You’ll need administrator permissions to edit files here, so make sure to open your text editor (like Notepad++) as an admin before proceeding.
Step 2: Backup the Original Template
Before making changes, copy the ConvertToFullProperty.cs.t4 file to a safe location—this way you can revert if something goes wrong.
Step 3: Modify the Template Content
Open the ConvertToFullProperty.cs.t4 file in your text editor. Here’s what you need to change:
1. Remove Underscore from Private Field
Find the line that defines the private field (it’ll look like this):
private $type$ _$fieldName$;
Change it to remove the underscore prefix:
private $type$ $fieldName$;
2. Switch to Multi-Line Property Format
Next, find the property definition section. The original single-line version looks like this:
public $type$ $propertyName$ { get { return _$fieldName$; } set { _$fieldName$ = value; } }
Replace it with this multi-line format, and also update the field reference to remove the underscore:
public $type$ $propertyName$ { get { return $fieldName$; } set { $fieldName$ = value; } }
(You can adjust the indentation here to match your team’s coding standards if needed.)
Step 4: Save and Test
Save the modified template file, then restart Visual Studio (changes won’t take effect until you do this).
To test:
- Create a C# class with an auto-property, e.g.,
public string Name { get; set; } - Right-click the property → Quick Actions and Refactorings → Convert to Full Property
- You should now see a private field without an underscore, and the property formatted across multiple lines.
Quick Notes
- If you can’t find the template file, double-check your VS installation path—some custom installs might place it in a different location.
- If changes don’t show up, try clearing the VS template cache by deleting the contents of
%LocalAppData%\Microsoft\VisualStudio\15.0_<Hash>\ItemTemplatesCache(replace<Hash>with your specific cache folder name).
内容的提问来源于stack exchange,提问作者Jan Paolo Go

