This post combines two of my favorite things: meta-programming and the elimination of magic strings. The goal is to automatically generate a class with
static const strings containing the name of all the CSS classes in your CSS files. Why would you want to do this? There are a number of reasons. First, it helps eliminate magic strings from your view code. Instead of writing
<p class="my-class"> you can write
<p class="@Css.MyClass">. It also helps when writing view code because you'll have access to IntelliSense data for all of your CSS classes, making it easier to remember their names and avoid mistakes. Finally, it improves analysis and refactoring because you can now rely on code engines to locate and operate on uses of a particular
const string instead of just plain-text searching.
This a technique for working with icon fonts, which have been steadily gaining in popularity. I love icon fonts. They allow me to package up a whole bunch of simple glyphs and pictograms, use them on my site or application without too much fuss on nearly every browser, and let me control presentation attributes such as color, size, etc. I especially like the recent trend of web-based tools for building custom icon fonts from an available library of glyphs (I tend to use Fontastic, but I've also had good luck with IcoMoon and FlatIcon).