locked
Cast Generic Type to Enum RRS feed

  • Question

  • Hello,

    I have the following method:

    public SelectList GetEnumListTask<T> where T : struct, IConvertible { }

    How can I cast the generic type T to an enum? I then need to do the following:

    new SelectList(Enum.GetValues(typeof(AccessLevel)).Cast<AccessLevel>().Select(x => new { Value = (Int32), Text = x.GetAttribute<DescrptionAttribute>() }));

    The problem is that in this case I want to use a generic type so I can apply this to any enum.

    Thank You,

    Miguel

    Friday, February 15, 2013 11:34 AM

Answers

  • Try this: Enum.GetValues(typeof(T)).Cast<T>().Select(x => new { Value = x, . . .

    • Proposed as answer by Jason Dot Wang Wednesday, February 20, 2013 6:45 AM
    • Marked as answer by Jason Dot Wang Monday, February 25, 2013 8:28 AM
    Friday, February 15, 2013 12:34 PM