Try our conversational search powered by Generative AI!

Henrik Fransas
Nov 13, 2015
  4467
(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
New Security Improvement released for Optimizely CMS 11

A new security improvement has been released for Optimizely CMS 11. You should update now!

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Azure AI Language – Key Phrase Extraction in Optimizely CMS

In this article, I demonstrate how the key phrase extraction feature, offered by the Azure AI Language service, can be used to generate a list of k...

Anil Patel | May 7, 2024 | Syndicated blog

Webinar: Get Started with AI within Optimizely CMS

Join us for the webinar "Get Started with AI in Optimizely CMS" on Wednesday, May 8th. Don't forget to register!

Luc Gosso (MVP) | May 7, 2024 | Syndicated blog

Search & Navigation: Indexing job new features

From Episerver.Find version 16.1.0, we introduced some new features that make the indexing job in CMS more flexible and efficient: Support continuo...

Vinh Cao | May 7, 2024

Exclude content from search engines

Best practices for excluding your Optimizely CMS test content from search engines like Google, along with some tips on what to do—and what not to...

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Run imgproxy container on Azure App Service with Front Door CDN

A simple way to host a imgproxy Docker container and use it to get AVIF and WEBP images.

Johan Kronberg | May 5, 2024 | Syndicated blog