Home > Code Generation > CodeRush Dynamic templates and dynamic lists

CodeRush Dynamic templates and dynamic lists

October 23rd, 2012

Template dynamic lists make a single code template universal and dynamic. For instance, a template for a variable declaration can be configurable on the fly by specifying its type. In other words, a template can be expanded differently depending on the selected dynamic list. A dynamic list contains a list of possible template alternative expansions (types when used with a variable template) for a single template.

Here are some of the standard dynamic lists:

  • System .NET types (e.g., System.Boolean, System.Char, System.Int32, System.DateTime, System.Exception, System.IO.StreamReader, etc.)
  • Generic Types with one param (e.g., System.Collections.Generic.List)
  • Generic Types with two params (e.g., System.Collections.Generic.Dictionary)
  • WPF Types (e.g., System.Windows.Media.Color, System.Windows.DependencyObject)
  • etc

Each dynamic list has a caption, variable name and a comment. The variable name identifies a dynamic list and is specified for a static template to make it dynamic. When you create a dynamic template, a variable name of the dynamic list is used in combination with the static template mnemonic as follows:



  • TM – Template Mnemonic
  • DLVN – Dynamic List Variable Name

Several dynamic lists may have the same variable name for identification but different captions for better dynamic list organization on the Dynamic Lists option page in the Options Dialog.

A dynamic list contains entries with Key and Value fields. A Key specifies a dynamic entry mnemonic name used in combination with the static template mnemonic. To expand a dynamic template, use the Template Mnemonic (TM) with a dynamic entry Key name. A Value is the entry value used in the template expansion. Consider the following dynamic list entries:



c System.Char
i System.Int32
s System.String
\ Paste the Clipboard content
/ Insert the current type name
? Insert the current method return type

Now, remember the ‘m’ template for methods. Combining the ‘m’ mnemonic with the Key dynamic list entries, we can expand the following methods:

mc – a method returning a value of the char type;
mi – a method returning a value of the integer type;
ms – a method returning a value of the string type;
m\ – a method returning a value of the type on the Clipboard;
m/ – a method returning a value of the current type;

Here, a single template ‘m’ can have as many different expansions as dynamic list entries that exist. Imagine that other templates may reuse this dynamic list and dynamic list entries are configurable and extensible. This makes the CodeRush templates collection grow dramatically reflecting the number of available code templates.

Don’t forget that you can add dynamic lists not only on the Dynamic Lists option page, but also right from the code editor window. Right-click the type declaration name or a type reference and select the Use Type In Templates… menu item:

CodeRush Use Type In Templates Context Menu

Then, specify the dynamic list entry mnemonic:

CodeRush Use Type In Templates Mnemonic Dialog

And you are done! Now, you can use this mnemonic in combination with all other existing standard and custom code templates. Custom mnemonics are added into the corresponding Custom Types dynamic list.

A great helper to remember static and dynamic template is to dock the CodeRush Training Window, which lists all available code templates in the current context.

Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Oct/23/2012
ID: C205

Similar Posts:

  1. No comments yet. Be the first and leave a comment!