wp7之Enum的GetNames方法

由于wp7没有实现Enum的GetNames方法。所以要自己实现。以下代码:

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<string>();
    }
}

以下是使用方法:

public enum em_口味
{
    例牌, 偏甜, 偏咸, 重辣, 微辣, 不要姜, 不要蒜, 不要葱, 素食主义, 穆斯林
}

var str = Enum<em_口味>.GetNames();

原文地址:https://www.cnblogs.com/jacle169/p/2845749.html