C# – Code generator tool to generate a property and backing field


I'm working in VS2008 and C#, and I'm looking for a (free) code generator tool to generate a property with getter and setter, as well as the backing private field to go with. The template thingy in VS does not make the field to go with it. Just looking for something a little bit better.

I once saw a web site where you could build this code, then cust-and-paste it from the web page to your code.

Best Solution

You can create custom snippets to do pretty much anything you want. Here is one I used in VS2005 for creating properties with backing fields:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
                    <!-- the shortcut below will show in your intellisense 
                         window - set it to whatever you wish -->
            <Description>Code snippet for a property</Description>
                    <ToolTip>property type</ToolTip>
                    <ToolTip>private field name</ToolTip>
                    <ToolTip>property name</ToolTip>
            <Code Language="csharp">
                    <![CDATA[$type$ $pname$;

            public $type$ $name$
                get { return this.$pname$; }
                set { this.$pname$ = value; }

Save this in a file called whatever.snippet in this location:

"C:\Documents and Settings\<YOU>\My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets"