Try our conversational search powered by Generative AI!

Henrik Fransas
Nov 13, 2015
  4464
(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
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog

Anonymous Tracking Across Devices with Optimizely ODP

An article by Lead Integration Developer, Daniel Copping In this article, I’ll describe how you can use the Optimizely Data Platform (ODP) to...

Daniel Copping | Apr 30, 2024 | Syndicated blog

Optimizely Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024