Reputation: 5900
I'm trying to make a generic method from this method:
public static SelectList LanguagesToSelectList()
{
return new SelectList(
Enum.GetValues(typeof(Languages))
.Cast<Languages>()
.Select(g => new KeyValuePair<Languages, string>(
g,
Resources.Views_GamesAndApplications.ResourceManager.GetString("Language_" + g.ToString()
)
)),
"Key",
"Value"
);
}
Here's what I got:
public static SelectList ToSelectList(Enum enumType, ResourceManager resourceManager, string resourcePrefix)
{
return new SelectList(
Enum.GetValues(typeof(enumType))
.Cast<enumType>()
.Select(g => new KeyValuePair<enumType, string>(
g,
resourceManager.GetString(resourcePrefix + g.ToString())
)),
"Key",
"Value");
}
However, enumType shouldn't be of type Enum
(nor should it be of type Type
), and I can't figure out of what type is SHOULD be, or if I should rephrase the entire method..
Usage example (compliant with given answer):
@Html.DropDownListFor(
m => m.Language,
SelectListHelper.ToSelectList<Languages>
(Resources.Views_GamesAndApplications.ResourceManager,"Language_"))
Thanks.
Upvotes: 0
Views: 168
Reputation: 1750
public static SelectList ToSelectList<T>(ResourceManager resourceManager, string resourcePrefix)
where T : struct
{
return new SelectList(Enum.GetValues(typeof(T)).Cast<T>()
.Select(g => new KeyValuePair<T, string>(g, resourceManager.GetString(resourcePrefix + g.ToString()))), "Key", "Value");
}
//Example:
var list = ToSelectList<Languages>(someResourceManager, "somePrefix");
Upvotes: 3