It has really been a source of great pain, that “someone” forgot to put in the Enum.GetNames() method in the SL-subset of .NET. Only the Enum.GetName() is present!
Anyway – a utility class I found in a blog comment somewhere came in quite handy:
public class Enum<T>
{
public static IEnumerable<string> GetNames()
{
var type = typeof(T);
if (!type.IsEnum)
throw new ArgumentException("Type '" + type.Name + "' is not an enum");
return (from field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
where field.IsLiteral
select field.Name).ToList();
}
}
You use it like this:
var names = Enum<StockExchange>.GetNames();
ExchangeNames.AddRange(names);
(here the StockExchange is an enum.)
Technorati Tags: WP7
1 comment:
It has really been a source of great pain ACMP Certification, that “someone” forgot to put in the Enum.GetNames() method in the SL-subset of .NET HP Certification Certification. Only the Enum.GetName() is present HPE ASE Storage Solutions Architect V2 Certification!
Post a comment