Views: 3044
Number of votes: 4
Average rating:

Create a prop-snippet to support property with virtual

When I am developing a site and creating a pagetype or block type I use the shortcut for creating a property inside Visual Studio a lot. The problem with that one is that is does not include the virtual part so I often forget that and without setting a property to virtual EPiServer will not use it.

These shortcuts are called snippets and are realy easy to create you own of. If you have Visual studio 2015 and using csharp you can find a lot of the snippets here:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#

To create your own epi-version of the prop shortcut just copy the one that is called prop.snippet and rename it to epiprop. Then open it and make shore it looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="">
	<CodeSnippet Format="1.0.0">
			<Description>Code snippet for an automatically implemented property
Language Version: C# 3.0 or higher</Description>
			<Author>Microsoft Corporation</Author>
					<ToolTip>Sort Order</ToolTip>
					<ToolTip>Property Name</ToolTip>
					<ToolTip>Property type</ToolTip>
					<ToolTip>Property name</ToolTip>
			<Code Language="csharp"><![CDATA[[Display(
			Name = "$name$"
			GroupName = SystemTabNames.Content,
			Order = $order$)]
			public virtual $type$ $property$ { get; set; }$end$]]>

This will give you a shortcut that if you write (after restarting Visual Studio, this is important) epiprop and press tab will look like this:

            Name = "Name"
            GroupName = SystemTabNames.Content,
            Order = 10)]
        public virtual int MyProperty { get; set; }

I hope this will help all others that aslo forget virtual sometimes

Nov 13, 2015

( By valdis, 11/13/2015 7:06:41 AM)

there should be a Roslyn analyzer that checks this during compilation time :)

( 11/13/2015 8:07:01 AM)

Nice! I've internally commented on this many times, but never bothered to look how to change/add such a shortcut.

Per Magne Skuseth
( By Per Magne Skuseth, 11/13/2015 8:54:44 AM)

Tip: Resharper has a handy features called Template Explorer. This makes it really easy to add and edit snippets and code templates.
Resharper -> Tools -> Templates Explorer.

Henrik Fransas
( By Henrik Fransas, 11/14/2015 5:25:09 AM)


True Per Magne, R# has a lot of good stuff :)

Petra Liljecrantz
( By Petra Liljecrantz, 11/20/2015 8:31:22 AM)

Good stuff! Thanks for sharing! :)

Please login to comment.