Help converting a helper method to an extension

Below is a helper method for converting Enums into a list for binding purposes.

public static List<Item<Enum, string>> ToList<T>() where T : struct, IConvertible
{
    if (!typeof(T).IsEnum)
    {
        throw new ArgumentException("T must be an enumerated type");
    }

    return Enum.GetValues(typeof(T))
               .Cast<Enum>()
               .Select(value => new Item<Enum, string>
               {
                  Description = (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                  Value = value
               })
               .OrderBy(item => item.Description)
               .ToList();
}

Open in new window


The helper method works but I am having trouble converting it to an extension. My extension is below:

public static List<Item<Enum, string>> ToList<T>(this T myEnum) where T : struct, IConvertible
{
    if (!typeof(T).IsEnum)
    {
        throw new ArgumentException("T must be an enumerated type");
    }

    var list = Enum.GetValues(typeof(T))
                   .Cast<Enum>()
                   .Select(value => new 
                   {
                       (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                       value
                   })
                   .OrderBy(item => item.Description)
                   .ToList();

    return null;
}

Open in new window


public class Item<TValue, TDisplay>
{
    public Item()
    {
    }

    public Item(TValue value, TDisplay description )
    {
        Value = value;
        Description = description ;
    }

    public TValue Value { get; set; }

    public TDisplay Description { get; set; }
}

Open in new window

matthew phungAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
"The helper method works but I am having trouble converting it to an extension"
What problems are you having?  Are you getting errors?
matthew phungAuthor Commented:
The ToList function in the extension is causing the following error :

The type 'System.Linq.IOrderedEnumerable<AnonymousType#1>' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Common.Extensions.EnumExtensions.ToList<T>(T)'      

Thank you in advance!
louisfrCommented:
There is a ToList method directly accessible in the current namespace. The compiler won't go farther. If you explicitly state the generic parameters of the ToList call, it should work better. For this you need to put back the Item object creation instead of an anonymous type:
return Enum.GetValues(typeof(T))
            .Cast<Enum>()
            .Select(value => new Item<Enum, string>
            {
                Description = (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                Value = value
            })
            .OrderBy(item => item.Description)
            .ToList<Item<Enum, string>>();

Open in new window

Bob LearnedCommented:
I would try the null character (?)

 public static List<KeyValuePair<Enum, string>> ToList<T>(this T? myEnum) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }

            var list = Enum.GetValues(typeof(T))
                           .Cast<Enum>()
                           .Select(value => new KeyValuePair<Enum, string>(
                                value,
                               (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()),
                                    typeof(DescriptionAttribute)) as DescriptionAttribute).Description
                           ))
                           .OrderBy(item => item.Key)
                           .ToList();

            return list;
        }

Note:  I changed the Item to KeyValuePair, so that I could get the compiler error to go away.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.