Jon Tackabury
Jon Tackabury

Reputation: 49247

C# code generation / removing redundant code

I have some simple code in a class:

private ITemplate _content1 = null;
[TemplateContainer(typeof(ContentContainer))]
public ITemplate Content1
{
    get
    { return _content1; }
    set
    { _content1 = value; }
}

I need about 15 of these content containers. I could just copy and paste this block 15 times and change the number, but there must be a better way. Can anyone suggest a cleaner way to do this?

Upvotes: 0

Views: 286

Answers (3)

Paul Sasik
Paul Sasik

Reputation: 81479

Why not use an collection of containers such as List for example? It seems that the only think you're changing is the integer index, using a List would make sense.

Upvotes: 1

Chris Brandsma
Chris Brandsma

Reputation: 11736

There is a property (prop) Snippet (snippets are native to visual studio).

Either modify the snippet (it is a simple xml file), or create a new one for your task.

ReSharper has a easier way, called code templates.

Or, generate the properties you need with a t4 script. But that is probably overkill.

Upvotes: 1

Spencer Ruport
Spencer Ruport

Reputation: 35117

Try this instead:

[TemplateContainer(typeof(ContentContainer))]
public ITemplate Content1
{ get; set; }

Upvotes: 3

Related Questions