Passing an enum as a parameter to a generic method.

I have a number of Enums that I decorate with display names, e.g.,

 public enum WorkflowStatus { [Display(Name = "In Setup")] InSetup = 1, [Display(Name = "In Progress")] InProgress = 2, ... } 

I currently have individual methods for each enum to grab the display name, e.g.

 public static string GetEnumDisplayName_WorkflowStatus(int? id) { return Enum.GetName(typeof(Enums.WorkflowStatus), id); } 

I'm trying to consolidate them into a single generic method. So far I have:

 public static string GetDisplayName<T>(T e, int enumId) { return Enum.GetName(typeof(**e**), enumId); } 

I am getting an error on the e in the typeof segment:

The type or namespace name 'e' could not be found (are you missing a using directive or an assembly reference?) 

I'd like to be able to call this method using something like:

 string name = Enums.GetDisplayName(Enums.WorkflowStatus, 1); 

Any ideas?

by nufsven via /r/csharp

Leave a Reply