Back to Devexpress

Iterate Through Elements

coderushforroslyn-116555-coding-assistance-custom-template-creation-iterate-through-elements.md

latest4.6 KB
Original Source

Iterate Through Elements

  • Dec 15, 2021
  • 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.

Follow the steps below to complete the task:

Open Templates Options Page

Choose the CodeRush | Code Templates… 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:

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.

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

CodeRush adds the newly created template to the 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:

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.

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

  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.

See Also

Providing Input Fields