HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Extension method to list enum values

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
methodenumextensionvalueslist

Problem

I have the following enum extension method:

public static IList SelectListFor()
{
  Type enumType = typeof(T);

  if (enumType.IsEnum)
  {
    return Enum.GetValues(enumType)
    .Cast()
    .Where(i => !i.Equals(0))
    .Select(e => new SelectListItem()
            {
            Value = e.ToString(),
      Text = GetDisplayName(enumType, Enum.GetName(enumType, e))
    })
    .ToList();
  }

  return null;
}


I can call this by doing

EnumExtensions.SelectListFor()


And it will allow me to create a drop down list for the values in my BrochureTypes enum

However, when I compile the code, I get the following warning:


CA1004 Consider a design where 'EnumExtensions.SelectListFor()' doesn't require explicit type parameter 'T' in any call to it.

Can I somehow change the above code to overcome this warning? Or should I just suppress the warning?

Solution

As written in the relative MSDN page, the cause of the warning is


The parameter signature of an externally visible generic method does not contain types that correspond to all the type parameters of the method.

Looking at your code, you get the type of the generic param into the method body (which can be seen also as a violation of the Separation of Concerns principle). In order to solve the issue, you can pass the type as a parameter. The resulting code should be something like the following:

public static IList SelectListFor(Type enumType)
{
    if (enumType.IsEnum)
    {
        return Enum.GetValues(enumType)
            .Cast()
            .Where(i => !i.Equals(0))
            .Select(e => new SelectListItem()
            {
                Value = e.ToString(),
                Text = GetDisplayName(enumType, Enum.GetName(enumType, e))
            })
            .ToList();
    }

    return null;
}


and can be called in the following way:

EnumExtensions.SelectListFor(typeof(BrochureTypes))


As an alternative, you can make it an extension method of the Type class, and I'd suggest a rename of the method in this case:

public static IList GetSelectList(this Type enumType)
{
    if (enumType.IsEnum)
    {
        return Enum.GetValues(enumType)
            .Cast()
            .Where(i => !i.Equals(0))
            .Select(e => new SelectListItem()
            {
                Value = e.ToString(),
                Text = GetDisplayName(enumType, Enum.GetName(enumType, e))
            })
            .ToList();
    }

    return null;
}


and call it like this:

(typeof(BrochureTypes)).GetSelectList();


or:

enumInstance.GetType().GetSelectList();

Code Snippets

public static IList<SelectListItem> SelectListFor(Type enumType)
{
    if (enumType.IsEnum)
    {
        return Enum.GetValues(enumType)
            .Cast<int>()
            .Where(i => !i.Equals(0))
            .Select(e => new SelectListItem()
            {
                Value = e.ToString(),
                Text = GetDisplayName(enumType, Enum.GetName(enumType, e))
            })
            .ToList();
    }

    return null;
}
EnumExtensions.SelectListFor(typeof(BrochureTypes))
public static IList<SelectListItem> GetSelectList(this Type enumType)
{
    if (enumType.IsEnum)
    {
        return Enum.GetValues(enumType)
            .Cast<int>()
            .Where(i => !i.Equals(0))
            .Select(e => new SelectListItem()
            {
                Value = e.ToString(),
                Text = GetDisplayName(enumType, Enum.GetName(enumType, e))
            })
            .ToList();
    }

    return null;
}
(typeof(BrochureTypes)).GetSelectList();
enumInstance.GetType().GetSelectList();

Context

StackExchange Code Review Q#116656, answer score: 8

Revisions (0)

No revisions yet.