Search code examples
c#typecast-operator

C# Explicit Type Casting Enumeration


I need to create a List of object of whatever Enumeration type is passed into the function below. I don't know what type it will be, but it can be any one of many possible enumerations in my project.

public static List<object> CreateEnumList(Enum enumeration)
{ 
    List<object> returnList = new List<object>();
    for (int enumIndex = 0; enumIndex < Enum.GetNames(enumeration.GetType()).Length; enumIndex++)
        returnList.Add((enumeration.GetType())enumIndex);
    return returnList;
}

How can I get the type cast to work correctly? The return value MUST be List of objects. Thank you


Solution

  • This is enough

    public static List<object> CreateEnumList(Enum enumeration)
    { 
        return Enum.GetValues(enumeration.GetType()).Cast<object>().ToList();
    }