Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code generator tool to generate a property and backing field

Tags:

c#

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.

like image 731
MattSlay Avatar asked Dec 05 '25 06:12

MattSlay


1 Answers

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">
        <Header>
            <Title>prop</Title>
                    <!-- the shortcut below will show in your intellisense 
                         window - set it to whatever you wish -->
            <Shortcut>_prop</Shortcut>
            <Description>Code snippet for a property</Description>
            <Author>Andrew</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <Default>String</Default>
                    <ToolTip>property type</ToolTip>
                </Literal>
                <Literal>
                    <ID>pname</ID>
                    <Default>_name</Default>
                    <ToolTip>private field name</ToolTip>
                </Literal>
                <Literal>
                    <ID>name</ID>
                    <Default>Name</Default>
                    <ToolTip>property name</ToolTip>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                    <![CDATA[$type$ $pname$;

            public $type$ $name$
            {
                get { return this.$pname$; }
                set { this.$pname$ = value; }
            }$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

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"

like image 72
Andrew Hare Avatar answered Dec 07 '25 20:12

Andrew Hare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!