π Documentation: IrbyImaging.Utility/EnumExtensions.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class Diagram (UML)
- Key Functional Areas
- Error Handling & Return Types
- Sample Code Blocks & Usage Examples
- Best Practices
- Conclusion
π Overview & Purpose
EnumExtensions is a static helper class in the IrbyImaging.Utility namespace that provides extension methods for any Enum type.
It leverages the [Display] attribute from System.ComponentModel.DataAnnotations to retrieve human-friendly
Name and Description values at runtime. This is especially useful for:
- Populating UI dropdowns or labels with friendly text
- Logging or reporting enums with rich, descriptive text
- Avoiding hard-coded
ToString()calls and making displays configurable
π οΈ Dependencies & Technologies
| Technology/Library | Purpose |
|---|---|
| .NET (C#) | Language & runtime |
| System.ComponentModel.DataAnnotations | Provides [Display] attribute metadata |
| System.Reflection | Used for inspecting enum fields and retrieving attributes |
Namespace:
IrbyImaging.Utility
Assembly:IrbyImaging.BLUtility
Author: kvamshi (Created 05-17-2022)
π Class Diagram (UML)
βββββββββββββββββββββββββββ
β Β«staticΒ» EnumExtensions β
βββββββββββββββββββββββββββ
β + GetDisplayName(e:Enum): string β
β + GetDescription(e:Enum): string β
β βββββββββββββββββββββββ β
β - GetDisplayAttribute(o:Object): β
β DisplayAttribute β
βββββββββββββββββββββββββββ
π Key Functional Areas
1. GetDisplayName
public static string GetDisplayName(this Enum enu)
- Purpose: Returns the
Namefrom the[Display(Name = "...")]attribute
if present; otherwise falls back toenu.ToString(). - Return:
stringβ friendly name.
2. GetDescription
public static string GetDescription(this Enum enu)
- Purpose: Returns the
Descriptionfrom the[Display(Description = "...")]attribute
if present; otherwise falls back toenu.ToString(). - Return:
stringβ descriptive text.
3. GetDisplayAttribute (Helper)
private static DisplayAttribute GetDisplayAttribute(object value)
- Purpose: Core helper that retrieves the
[Display]attribute instance
from the enum field via Reflection. - Exceptions:
- Throws
ArgumentExceptionif the providedvalueis not an enum.
- Throws
- Return:
DisplayAttributeornullif not applied.
β οΈ Error Handling & Return Types
| Method | Throws | Return Value if No Attribute |
|---|---|---|
| GetDisplayName | None (handles null attr) | enu.ToString() |
| GetDescription | None (handles null attr) | enu.ToString() |
| GetDisplayAttribute | ArgumentException |
β |
- ArgumentException message format:
"Type {0} is not an enum"
π» Sample Code Blocks & Usage Examples
-
Define an enum with
[Display]using System.ComponentModel.DataAnnotations; public enum OrderStatus { [Display(Name = "Pending Approval", Description = "Waiting for manager approval")] Pending, [Display(Name = "In Process", Description = "Currently being processed")] Processing, [Display(Name = "Completed", Description = "Order has been completed successfully")] Completed, Canceled // No Display attributeβfalls back to "Canceled" } -
Retrieve display attributes
using IrbyImaging.Utility; var status = OrderStatus.Processing; string name = status.GetDisplayName(); // "In Process" string desc = status.GetDescription(); // "Currently being processed" var raw = OrderStatus.Canceled; Console.WriteLine(raw.GetDisplayName()); // "Canceled" Console.WriteLine(raw.GetDescription()); // "Canceled" -
Handling invalid usage
object notEnum = "Hello"; try { // This will throw ArgumentException var attr = EnumExtensions.GetDisplayAttribute(notEnum); } catch (ArgumentException ex) { Console.Error.WriteLine(ex.Message); // Output: "Type System.String is not an enum" }
π‘ Best Practices
- Always decorate your enums with
[Display]for user-facing applications. - Use
GetDisplayNamefor labels/texts andGetDescriptionfor tooltips or detailed info. - Catch and log
ArgumentExceptionif attempting to retrieve display data on non-enum objects.
π Conclusion
The EnumExtensions utility provides a clean, reusable way to access
custom display metadata on enums, promoting separation of concerns
between your domain logic and presentation layer. Simply apply
[Display(Name="", Description="")] attributes to any enum values
to automatically harness these extension methods throughout your codebase.
Happy coding! π