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.
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
In the Templates page, create a template within a category. For example, select the “Custom Templates” category and click New Template.
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 |
---|---|---|---|
|
|
|
|
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
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.
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:
Click Apply and OK to save these templates and close the Templates options page.
Expand the Template
Open a project.
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.