πŸ“‘ Documentation: IrbyImaging.Utility/EnumExtensions.cs


πŸ“‹ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Diagram (UML)
  4. Key Functional Areas
  5. Error Handling & Return Types
  6. Sample Code Blocks & Usage Examples
  7. Best Practices
  8. 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 Name from the [Display(Name = "...")] attribute
    if present; otherwise falls back to enu.ToString().
  • Return: string – friendly name.

2. GetDescription

public static string GetDescription(this Enum enu)
  • Purpose: Returns the Description from the [Display(Description = "...")] attribute
    if present; otherwise falls back to enu.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 ArgumentException if the provided value is not an enum.
  • Return: DisplayAttribute or null if 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

  1. 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"
    }
    
  2. 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"
    
  3. 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 GetDisplayName for labels/texts and GetDescription for tooltips or detailed info.
  • Catch and log ArgumentException if 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! πŸŽ‰