Skip to main content

Iterate Through Elements

  • 4 minutes to read

CodeRush allows you to create a template that can perform the following actions with code elements when you expand it:

  • Iterate through elements (for example, class members, enumeration elements, etc.).

  • Apply a template to each element.

  • Concatenate the results.

This topic describes how to create the “map” template that generates property mapping to DTO.

Ext_Templates_Iterate3_GIF

Follow the steps below to complete the task:

Open Templates Options Page

Choose the CodeRush | Code Templates… menu item.

Template Menu Item

Note

In Visual Studio 2019, CodeRush menu is placed into a new location - in Visual Studio Extensions menu. You can reposition the CodeRush menu back to the top level of the Visual Studio menu bar in Visual Studio 2019 version 16.4 or later. See the following topic for more information: First Steps.

CodeRush shows the Templates page:

Template Page

Create a Template

  1. In the Templates page, create a template within a category. For example, select the “Custom Templates” category and click New Template.

    Create Template

  2. Enter the template name and click OK in the New Template dialog.

    Map Creation

CodeRush adds the newly created template to the category.

New Template in Category

Specify a Text Command for Iteration

The text command used for iteration is «ForEach(iteration_bounds,template)».

The iteration_bounds parameter is a string built according to the following syntax:

[visibility] item in scope

The iteration_bounds string elements can have the following values:

visibility (optional)

item

keyword

scope

  • private
  • protected
  • internal (Friend in VB)
  • protectedlnternal
  • public
  • abstract (Mustlnherit in VB)
  • element (Node in VB)
  • parameter
  • local
  • field
  • enumElement
  • member
  • method
  • property
  • event
  • class
  • struct
  • interface
  • enum
  • namespace
  • line (a line in multi-line text)
  • in
  • block (current code block)
  • itemElement
  • method
  • property
  • this (Me in VB)
  • thisAndBase (symbols declared in the type and its base type hierarchy)
  • base ((Inherited in VB) all ancestors of the active class)
  • objectCreationType (a type symbol of the object initializer expression)
  • objectCreationTypeAndBase (a type symbol and base type symbol of the object initializer expression)
  • clipClass (a type name from the clipboard)
  • clipType (an identifier type from the clipboard)
  • clipTypeAndBase (an identifier type and its base type from the clipboard)
  • clipboardText
  • file

Note

You can expand a template with “enumElement in clipType“ bounds when the clipboard contains an enumeration name.

In this example, copy the following text command and paste it into the “map” expansion code:

«ForEach(public property in objectCreationType, #mapItem#)»” - iterates through public properties of the object initializer expression.

The following screenshot shows the inserted text command:

Iteration Templates Text Command

The “#mapItem#“ is a target template that CodeRush applies to each iteration element when you expand the “map” template.

Create a Target Template and Customize It

  1. Create the “#mapItem#“ template in the “Custom Templates” catalog. See the Create a Template section for information on how to do it.

    The target template can access the following variables:

    • itemType - the type of the current iteration element (can be empty).
    • itemFullType - the fully-qualified type of the current iteration element.

    • itemName - the current iteration element.

    To insert the values of these variables into your target template, you can use the «?Get(itemType)», «?Get(itemFullType)», and «?Get(itemName)» text commands. See the following topic for more information: Using Variables.

  2. Copy and paste the following expansion code to the “#mapItem#“ template:

    «?Get(itemName)» = «BlockAnchor»«Link(name)»«Caret».«?Get(itemName)»,_

    This expansion code contains the following text commands:

    • «?Get(itemName)» - inserts the values of the current iteration element.
    • «BlockAnchor» - marks the end of a text selection that starts from the caret position.
    • «Link(name)» - links “name” identifiers. If you change a linked identifier after template expansion, CodeRush changes the associated identifiers.
    • «Caret» - places the code editor’s caret at the point where this text command appears in the template expansion code.

    The screenshot below shows the inserted expansion code for the “#mapItem#“ template:

    Ext_Templates_Iterate2

  3. Click Apply and OK to save these templates and close the Templates options page.

Expand the Template

  1. Open a project.

  2. Type the “map” template and press Space or Tab depending on your settings to expand the template.

The “map” template calls the #mapItem# template for each public property of the object initializer expression. You can change the identifier name after the template expansion.

Ext_Templates_Iterate3_GIF

See Also