How to: Group Interface Implementations and Wrap them into Regions
- 3 minutes to read
Organize Members can group explicit and implicit interface implementations and wrap them into regions according to the interface implementation rule. This example shows how to create, configure, and apply the interface implementations rule.
Add an Interface Implementations Rule to a Rule Set
Open the Editor | All Languages | Organize Members options page to configure Organize Members.
Click Add Rule to add a rule for interface implementations.
CodeRush adds the new rule to the end of the rule list.
Change the suggested rule name to “Interface implementations” and press Enter to save the change.
Change the “Interface implementations” rule’s position, as shown in the screencast below, so that Interface implementations follow indexers in a type. To change the rule’s position, drag the “Interface implementations” rule in the rule list and drop it when you reach the desired position.
Note
Organize Members applies rules according to the specified order in the rule set and the Rule priority setting.
Customize the Rule
Change the Rule Priority
Set the Rule Priority setting to High priority to allow Organize Members to wrap interface implementations into regions. For information about this setting, see the following topic: Rule Priority
Note
The Rule priority setting does not affect the rule order specified in the rule set.
Configure Grouping
This section describes how to specify a condition which is used to group interface implementations.
Select the “Interface implementations” rule in the rule list.
In the “Group by” section, click the “+” button to add a new condition to the “And” group.
CodeRush adds the “Node kind”.
Click the first “Node kind” and choose Is interface implementor from the list.
The And group looks as follows:
When you run Organize Members CodeRush analyzes all type’s members and checks the Is interface implementor condition to create an interface implementations group.
Configure Sorting
This section describes how to specify a condition which is used to sort interface implementation groups.
In the “Sort by” section, set the Sort by to InterfaceName. This allows Organize Members to sort interface implementation groups by interface name.
Enable the Wrap distinct groups in regions option to activate wrapping of implicit and explicit interface implementations in regions.
Type “{InterfaceName} implementations” in the region name text box.
Click Apply to save changes or click OK to save changes and close the Organize Members options page.
Run Organize Members
This section shows how to apply the interface implementation rule.
In the following code, place the caret to any place in the class body.
using System.Drawing;
namespace ConsoleApp {
interface IDimensions {
float GetLength();
float GetWidth();
}
interface IColor {
Color GetBackgroundColor();
Color GetForegroundColor();
}
public class Box : IDimensions, IColor {
Color boxColor;
float lengthInches;
float widthInches;
Box(float length, float width, Color color) {
lengthInches = length;
widthInches = width;
boxColor = color;
}
float GetLength() {
return lengthInches;
}
float IDimensions.GetLength() {
return lengthInches;
}
float GetWidth() {
return widthInches;
}
float IDimensions.GetWidth() {
return widthInches;
}
public Color GetForegroundColor() {
return boxColor;
}
public Color GetBackgroundColor() {
return boxColor;
}
}
}
Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu. Select Organize Members and press Enter.
CodeRush groups explicit and implicit interface implementations according to the specified rule and wraps these implementations into regions.