How to: Create Regions for Explicit Interface Implementations
- 2 minutes to read
CodeRush can wrap sorted groups in dynamic regions with built-in variables. This example shows how to wrap explicit interface implementations groups in regions.
Follow the steps below.
Copy the following code and paste it to your project.
using System; using System.Collections.Generic; using System.Runtime.Serialization; namespace Project { [Serializable] class PersonInfo : IDisposable, ISerializable, IEquatable<PersonInfo> { public string Name { get; set; } public int Age { get; set; } public string Id { get; set; } void IDisposable.Dispose() { } protected PersonInfo(SerializationInfo info, StreamingContext context) { if (info == null) return; Name = info.GetString(nameof(Name)); Age = info.GetInt32(nameof(Age)); Id = info.GetString(nameof(Id)); } void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) return; info.AddValue(nameof(Name), Name); info.AddValue(nameof(Age), Age); info.AddValue(nameof(Id), Id); } public override bool Equals(object obj) { if (obj is PersonInfo) return this.Equals((PersonInfo)obj); return base.Equals(obj); } public static bool operator ==(PersonInfo first, PersonInfo second) { if ((object)first == null) return (object)second == null; return first.Equals(second); } public static bool operator !=(PersonInfo first, PersonInfo second) { return !(first == second); } bool IEquatable<PersonInfo>.Equals(PersonInfo other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(Name, other.Name) && Age.Equals(other.Age) && Equals(Id, other.Id); } public override int GetHashCode() { unchecked { int hashCode = 47; if (Name != null) hashCode = hashCode * 53 ^ EqualityComparer<string>.Default.GetHashCode(Name); hashCode = hashCode * 53 ^ Age.GetHashCode(); if (Id != null) hashCode = hashCode * 53 ^ EqualityComparer<string>.Default.GetHashCode(Id); return hashCode; } } } }
Open the Editor | All Languages | Organize Members options page to configure the Member Organization rules feature.
Select the Explicit interface implementations rule from the member organization rules list.
Note
The default Rule priority setting is High priority for explicit interface implementations. This allows CodeRush to group these interface implementations prior to other rules.
In the “Sort by” section, enable the Wrap distinct groups in regions option to activate wrapping of explicit interface implementations in regions.
Type “{ExplicitInterfaceName} members” in the “Region name” text box.
Click Apply and OK to save changes and close the Organize Members options page.
Switch to the code editor.
Place the caret to any place in the class body.
Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu
Choose Organize Members from the list and press Enter.
CodeRush wraps explicit interface implementations in regions and places these regions in the beginning of the class.