遍历 Silverlight 中的枚举? [英] Iterating through an enumeration in Silverlight?

查看:21
本文介绍了遍历 Silverlight 中的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .Net 中,可以使用

In .Net it is possible to iterate through an enumeration by using

System.Enum.GetNames(typeof(MyEnum)) 

System.Enum.GetValues(typeof(MyEnum))

然而,在 Silverlight 3 中,没有定义 Enum.GetNames 和 Enum.GetValues.有人知道替代方案吗?

In Silverlight 3 however, Enum.GetNames and Enum.GetValues are not defined. Does anyone know an alternative?

推荐答案

我想出了如何做到这一点,而无需对枚举做出假设,模仿 .Net 中的功能:

I figured out how to do this without making assumptions about the enum, mimicking the functions in .Net:

public static string[] GetNames(this Enum e) {
    List<string> enumNames = new List<string>();

    foreach (FieldInfo fi in e.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)){
        enumNames.Add(fi.Name);
    }

    return enumNames.ToArray<string>();
}

public static Array GetValues(this Enum e) {
    List<int> enumValues = new List<int>();

    foreach (FieldInfo fi in e.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)) {
        enumValues.Add((int)Enum.Parse(e.GetType(), fi.Name, false));
    }

    return enumValues.ToArray();
}

这篇关于遍历 Silverlight 中的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆