So I am sitting here, feeding the cold I’ve had, and I found myself creating a DataContract – something that got even easier with the introduction of Automatic Properties in .NET 3.0.
I tend to forget about the Visual Studio feature of Code Snippets, but I am usually reminded again anytime I see a presentation by an experienced technical presenter – then I forget again.
Today, I happened to remember our last instructor making heavy use of the ‘prop’ expanding code snippet during demos and thought - “Hey, I bet they have one for automagically creating properties marked with the DataMember attribute”. Well, I didn’t immediately see it and asking the great information oracle didn’t reveal anything interesting, so I set out to copy the existing ‘prop’ code snippet.
Turns out it was even easier than I expected – it took longer to write this post than it did to create the snippet.
Save the following into a file named dmprop.snippet (or something suitable) and drop it into your C:\Users\xxxxx\Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets folder, and voila! Visual Studio should automatically detect the new snippet. If not, you can add manually via Tools | Code Snippets Manager.
Now you can type ‘dmprop’ [tab] [tab] and get the same property expansion code snippet as ‘prop’, but marked with a DataMember attribute. Cheesy, but I figured it was appropriate as I was snacking on some sharp cheddar..
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>dmprop</Title>
<Shortcut>dmprop</Shortcut>
<Description>Code snippet for an automatically implemented [DataMember] property</Description>
<Author>Your name here</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[[DataMember]
public $type$ $property$ { get; set; }$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Yes! Now I don't have to write this. Glad you went and did it :).
ReplyDeleteGood Stuff. Thanks
ReplyDelete