Refactorable Settings in Visual Studio, Part 3

Published on Sunday, August 17, 2014

Photo by Jan Loyde Cabrera on Unsplash

(Part 1, Part 2)

I wrote two different solutions to this problem. The first one emerged as I was exploring T4 templates as a possible solution. My first attempt fell short because I didn't have an acceptable way to run the T4 template when the settings file changed. I turns out that if you have the T4 Toolbox extension installed, you can set the custom tool for any file to T4Toolbox.ScriptFileGenerator1 and the extension will generate a blank .tt file. You can then fill it in with your code generation template and the template will be run whenever the file changes.

With the extension installed, replace SettingsSingleFileGenerator with T4Toolbox.ScriptFileGenerator, and then replace the contents of you .tt file with the template in this gist. The generated code will basically look exactly like what comes out of SettingsSingleFileGenerator, but with the addition of the settings names as constants.

If you're already using the T4 Toolbox, this is absolutely the method I'd recommend. And if you're already using T4 in your project, you might as well install the toolbox for the Intellisense and highlighting features2.

T4, by the way, is amazing. I had to write a couple of code generators about a decade ago (before T4 was a thing), and it's astounding how much easier it is with T4.

But what if you're not using T4 and/or don't want to install the T4 toolbox? I was kind of uncomfortable with the idea of having the toolbox installed when generating this settings code was literally the only thing I was using T4 for on my project. It just seemed like overkill. So I dusted off my decade-old code-generation skills and took a shot at writing my own SingleFileGenerator.

Last time I did this (in 2004), it was a nightmare (which is why I explored every other possible solution first). Getting it installed required a lot of manual deployment of libraries in the right location and editing of registry entries; testing required either another machine with VS installed or risking your development installation. Luckily, things are a lot easier nowadays. I found a great example project on creating a Single File Generator as a VS extension, which makes for a much easier deployment process. And you can debug them in a separate experimental instance of VS without any risk. Progress!

The result is the Refactorable Settings Generator extension. Install it and replace SettingsSingleFileGenerator with RefactorableSettingsGenerator.

example of settings gui

Again, the generated code will basically look exactly like what comes out of SettingsSingleFileGenerator, but with the addition of the settings names as constants.

Whichever solution you choose, your handlers can now be written like this:

Settings.Default.SettingChanging +=
    (sender, eventArgs) =>
        if (eventArgs.SettingName == Settings.SettingsNames.AutoCheckUpdates)
           // do something

And they will helpfully fail to compile when you change the names of the settings.

You can check out the code for the Refactorable Settings Generator on GitHub (and report issues or suggest changes, if you like).

Happy refactoring!

  1. At least, that's what it's called for Visual Studio 2012 and up; the tool might have a different name in VS 2010. 
  2. If you're using a dark theme in VS, you're going to want to go to the color options and change the background of `Text Template Code Block`, though - with the default colors the T4 code becomes unreadable.