JacobIRR
JacobIRR

Reputation: 8966

create dictionary from enum names and display names

I have a helper method to get the Display Name of an enum:

public static string GetDisplayName(this Enum @enum)
{
    return @enum.GetAttribute<DisplayAttribute>()?.GetName();
}

I'm trying to make a Dictionary out of the enum names as keys, with the values as the display names using this code:

public static Dictionary<string, string> EnumDisplayNameDictionary<TEnum>(this Enum @enum)
{
    var returnDict = new Dictionary<string, string>(); 
    foreach (var item in Enum.GetValues(typeof(TEnum)))
    {
        returnDict.Add(item.ToString(), @enum.GetDisplayName());
    }

    return returnDict;
}

Here is the code for one of my enums (with all its WCF magic included):

[DataMember]
[Column("cash_revenue_recognition")]
public byte CashRevenueRecognitionId
{
    get
    {
        return (byte)this.CashRevenueRecognition;
    }
    set
    {
        CashRevenueRecognition = (CashRevenueRecognitionChoices) value;
    }
}

[DataMember]
[EnumDataType(typeof(CashRevenueRecognitionChoices))]
public CashRevenueRecognitionChoices CashRevenueRecognition { get; set; }

[DataContract]
[Flags]
public enum CashRevenueRecognitionChoices
{
    [EnumMember]
    [Display(Name ="")]
    NotSet = 0,
    [EnumMember]
    [Display(Name = "When we invoice a customer for their payment")]
    CashAtInvoice = 1,
    [EnumMember]
    [Display(Name = "When we receive a customer payment in our bank account ")]
    CashAtPayment = 2
}

[DataMember]
public Dictionary<string, string> CashEnumDictionary =>
    CashRevenueRecognition.EnumDisplayNameDictionary<CashRevenueRecognitionChoices>();

The problem I'm having is that I don't know how to get the Display Name of the specific item I am currently referencing in my loop.

Actual Output:

{
    "NotSet" : "When we receive a customer payment in our bank account",
    "CashAtInvoice" : "When we receive a customer payment in our bank account",
    "CashAtPayment" : "When we receive a customer payment in our bank account"
}

Expected Output:

{
    "NotSet" : "",
    "CashAtInvoice" : "When we invoice a customer for their payment",
    "CashAtPayment" : "When we receive a customer payment in our bank account"
}

Upvotes: 1

Views: 878

Answers (2)

St. Pat
St. Pat

Reputation: 749

In this function, the Enum.GetValues call return a list of objects, which doesn't have a definition for GetDisplayName().

And the reason you see the same description for each one is that the @enum object is the only object you are using to get description

public static Dictionary<string, string> EnumDisplayNameDictionary<TEnum>(this Enum @enum)
{
    var returnDict = new Dictionary<string, string>(); 
    foreach (var item in Enum.GetValues(typeof(TEnum)))
    {
        returnDict.Add(item.ToString(), @enum.GetDisplayName());
    }

    return returnDict;
}

Simply cast item to Enum, and it will allow you to call GetDisplayName().

public static Dictionary<string, string> EnumDisplayNameDictionary<TEnum>(this Enum @enum)
{
    var returnDict = new Dictionary<string, string>(); 
    foreach (var item in Enum.GetValues(typeof(TEnum)))
    {
        returnDict.Add(item.ToString(), ((Enum)item).GetDisplayName());
    }

    return returnDict;
}

With this, the @enum object is not used within this function, so this could be changed to a static function, rather than an extension function.

Upvotes: 1

Fancy5g
Fancy5g

Reputation: 109

foreach (var item in Enum.GetValues(typeof(TEnum)))
{
    returnDict.Add(item.ToString(), item.GetDisplayName());
}

Upvotes: 0

Related Questions