Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Henrik Fransas
Nov 13, 2015
  4460
(4 votes)

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="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>epiprop</Title>
			<Shortcut>epiprop</Shortcut>
			<Description>Code snippet for an automatically implemented property
Language Version: C# 3.0 or higher</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>order</ID>
					<ToolTip>Sort Order</ToolTip>
					<Default>10</Default>
				</Literal>
				<Literal>
					<ID>name</ID>
					<ToolTip>Property Name</ToolTip>
					<Default>Name</Default>
				</Literal>
				<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[[Display(
			Name = "$name$"
			GroupName = SystemTabNames.Content,
			Order = $order$)]
			public virtual $type$ $property$ { get; set; }$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

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:

        [Display(
            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

Comments

valdis
valdis Nov 13, 2015 07:06 AM

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

Nov 13, 2015 08:07 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
Per Magne Skuseth Nov 13, 2015 08:54 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
Henrik Fransas Nov 14, 2015 05:25 AM

Thanks.

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

Petra Liljecrantz
Petra Liljecrantz Nov 20, 2015 08:31 AM

Good stuff! Thanks for sharing! :)

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog