Search code examples
c#interfaceenumsextension-methods

Behaviour to simulate an enum implementing an interface


Say I have an enum something like:

enum OrderStatus
{
    AwaitingAuthorization,
    InProduction,
    AwaitingDespatch
}

I've also created an extension method on my enum to tidy up the displayed values in the UI, so I have something like:

public static string ToDisplayString(this OrderStatus status)
{
    switch (status)
    {
        case Status.AwaitingAuthorization:
            return "Awaiting Authorization";

        case Status.InProduction:
            return "Item in Production";

        ... etc
    }
}

Inspired by the excellent post here, I want to bind my enums to a SelectList with an extension method:

public static SelectList ToSelectList<TEnum>(this TEnum enumObj)

however, to use the DisplayString values in the UI drop down I'd need to add a constraint along the lines of

: where TEnum has extension ToDisplayString

Obviously none of this is going to work at all with the current approach, unless there's some clever trick I don't know about.

Does anyone have any ideas about how I might be able to implement something like this?


Solution

  • Is there a compelling reason to use an enum here?

    When you start jumping through crazy hoops to use enums, it might be time to use a class.

    public class OrderStatus
    {
        OrderStatus(string display) { this.display = display; }
    
        string display;
    
        public override string ToString(){ return display; }
    
        public static readonly OrderStatus AwaitingAuthorization
            = new OrderStatus("Awaiting Authorization");
        public static readonly OrderStatus InProduction
            = new OrderStatus("Item in Production");
        public static readonly OrderStatus AwaitingDispatch
            = new OrderStatus("Awaiting Dispatch");
    }
    

    You consume it the same as an enum:

    public void AuthorizeAndSendToProduction(Order order, ProductionQueue queue)
    {
        if(order.Status != OrderStatus.AwaitingAuthorization) 
        {
            Console.WriteLine("This order is not awaiting authorization!");
            return;
        }
        order.Status = OrderStatus.InProduction;
        queue.Enqueue(order);
    }
    

    The string representation is built-in, and all you need is ToString().