如何在.Net 3.5中Enum.GetValues?

发布于 2024-12-26 18:06:56 字数 1419 浏览 3 评论 0 原文

在 .Net 4.0 中,我使用 System.Enum.GetValues(typeof(Gender)) 来获取枚举项的列表。
在一个完整的示例中,我通常以这种方式查找枚举值:

    Gender retVal = Gender.Male;

    foreach (Gender enumType in System.Enum.GetValues(typeof(Gender)))
    {
        if (enumType.ToString().Trim().ToUpper().Substring(0,1).Equals(stringToEnum.Trim().ToUpper()))
        {
            retVal = enumType;
            break;
        }
    }  

但是我如何在 .Net 3.5 (Pocket PC) 中执行此操作?
提前致谢 !

我使用下面的答案,但它对我不起作用。这是代码:

枚举值:

namespace Ionic.Zlib
{
    public enum CompressionLevel
    {
        Level0 = 0,
        None = 0,
        Level1 = 1,
        BestSpeed = 1,
        Level2 = 2,
        Level3 = 3,
        Level4 = 4,
        Level5 = 5,
        Level6 = 6,
        Default = 6,
        Level7 = 7,
        Level8 = 8,
        BestCompression = 9,
        Level9 = 9,
    }
}  

用法:

我只是错过了初始化新对象。这现在有效:

public static Ionic.Zlib.CompressionLevel GetCompressionLevel(string Expression)
{
    Ionic.Zlib.CompressionLevel result = Ionic.Zlib.CompressionLevel.None;
    foreach (Ionic.Zlib.CompressionLevel item in EnumGetValues(new Ionic.Zlib.CompressionLevel()))
    {
        if(object.Equals(item.ToString().Trim().ToUpper(), Expression.Trim().ToUpper()))
        {
            result = item;
            break;
        }
    }
    return result;
}

In .Net 4.0, I use System.Enum.GetValues(typeof(Gender)) to get the list of enum item.
In a complete example, I use to look for enum value in this way:

    Gender retVal = Gender.Male;

    foreach (Gender enumType in System.Enum.GetValues(typeof(Gender)))
    {
        if (enumType.ToString().Trim().ToUpper().Substring(0,1).Equals(stringToEnum.Trim().ToUpper()))
        {
            retVal = enumType;
            break;
        }
    }  

But how could I do this in .Net 3.5 (Pocket PC) ?
Thanks in advance !

I use the answers below but It doesn't work for me. Here's the code:

Enum Values:

namespace Ionic.Zlib
{
    public enum CompressionLevel
    {
        Level0 = 0,
        None = 0,
        Level1 = 1,
        BestSpeed = 1,
        Level2 = 2,
        Level3 = 3,
        Level4 = 4,
        Level5 = 5,
        Level6 = 6,
        Default = 6,
        Level7 = 7,
        Level8 = 8,
        BestCompression = 9,
        Level9 = 9,
    }
}  

Usage:

I just miss to initialize new object. This works now:

public static Ionic.Zlib.CompressionLevel GetCompressionLevel(string Expression)
{
    Ionic.Zlib.CompressionLevel result = Ionic.Zlib.CompressionLevel.None;
    foreach (Ionic.Zlib.CompressionLevel item in EnumGetValues(new Ionic.Zlib.CompressionLevel()))
    {
        if(object.Equals(item.ToString().Trim().ToUpper(), Expression.Trim().ToUpper()))
        {
            result = item;
            break;
        }
    }
    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2025-01-02 18:06:56

这里有一篇博客文章(存档此处) 通过反射实现它:

public IEnumerable<Enum> GetValues(Enum enumeration)
{
    List<Enum> enumerations = new List<Enum>();
    foreach (FieldInfo fieldInfo in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
    {
        enumerations.Add((Enum)fieldInfo.GetValue(enumeration));
    }
    return enumerations;
}

There's a blog post here (archived here) which achieves it via reflection:

public IEnumerable<Enum> GetValues(Enum enumeration)
{
    List<Enum> enumerations = new List<Enum>();
    foreach (FieldInfo fieldInfo in enumeration.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
    {
        enumerations.Add((Enum)fieldInfo.GetValue(enumeration));
    }
    return enumerations;
}
夕色琉璃 2025-01-02 18:06:56

.NET CF 3.5 不支持 System.Enum.GetValues,您应该找到另一种方法来迭代您的枚举,例如,看看这里:

http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/dbb6c1ff-935a-4fba-84a2-4cb6e892a61f/

System.Enum.GetValues is not supported on .NET CF 3.5, you should find another way to iterate on your enum, for example have a look here:

http://social.msdn.microsoft.com/forums/en-US/netfxcompact/thread/dbb6c1ff-935a-4fba-84a2-4cb6e892a61f/

心碎无痕… 2025-01-02 18:06:56

除了 shamp00 的答案之外,我建议创建一个带有泛型类型参数的静态方法。这样你就不需要将枚举类型的变量传递给方法:

public static class EnumHelper
{
    public static TEnum[] GetValues<TEnum>()
    {
        return typeof(TEnum)
            .GetFields(BindingFlags.Static | BindingFlags.Public)
            .Select(fieldInfo => (TEnum)fieldInfo.GetValue(null))
            .ToArray();
    }
}

通常我不喜欢以“Helper”结尾的类,但仅使用 Enum 会与内置冲突Enum-类。

要使用此方法,只需使用您的枚举类型调用它:

var values = EnumHelper.GetValues<MyEnum>();

In addition to shamp00's answer I would suggest to create a static method with a generic type parameter. That way you don't need to pass a variable of the enum type to the method:

public static class EnumHelper
{
    public static TEnum[] GetValues<TEnum>()
    {
        return typeof(TEnum)
            .GetFields(BindingFlags.Static | BindingFlags.Public)
            .Select(fieldInfo => (TEnum)fieldInfo.GetValue(null))
            .ToArray();
    }
}

Usually I don't like class ending on "Helper", but using just Enum would conflict with the built-in Enum-class.

To use this method just invoke it with your enum type:

var values = EnumHelper.GetValues<MyEnum>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文