Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Silverlight 4中迭代枚举_C#_Silverlight_Enums - Fatal编程技术网

C# 在Silverlight 4中迭代枚举

C# 在Silverlight 4中迭代枚举,c#,silverlight,enums,C#,Silverlight,Enums,可能重复: 在Silverlight中,有没有一种方法可以用C遍历枚举中的所有值 我知道WPF允许您使用System.Enum.GetTypeType方法,但这在Silverlight中不可用 谢谢, 赛斯试试这个: public static List<T> GetList<T>(Type enumType) { List<T> output = new List<T>(); var field

可能重复:

在Silverlight中,有没有一种方法可以用C遍历枚举中的所有值

我知道WPF允许您使用System.Enum.GetTypeType方法,但这在Silverlight中不可用

谢谢, 赛斯

试试这个:

    public static List<T> GetList<T>(Type enumType)
    {
        List<T> output = new List<T>();

        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;


        foreach (FieldInfo field in fields)
        {
            object value = field.GetValue(enumType);
            output.Add((T) value);
        }

        return output;

    }
可以这样称呼:

List<MyEnum> myList = GetList<MyEnum>(typeof(MyEnum))
试试这个:

    public static List<T> GetList<T>(Type enumType)
    {
        List<T> output = new List<T>();

        var fields = from field in enumType.GetFields()
                     where field.IsLiteral
                     select field;


        foreach (FieldInfo field in fields)
        {
            object value = field.GetValue(enumType);
            output.Add((T) value);
        }

        return output;

    }
可以这样称呼:

List<MyEnum> myList = GetList<MyEnum>(typeof(MyEnum))
用法

用法


@阿尼-你是对的,我错过了那个问题。@阿尼-你是对的,我错过了那个问题。