如何传递枚举作为参数

发布于 2024-12-07 19:19:56 字数 823 浏览 1 评论 0原文

可能的重复:
在 Silverlight 中迭代枚举?

我想出了一个方便的小函数让我得到枚举的计数(我知道它不能与所有枚举一起正常工作)。我不想将枚举硬编码到函数中,这样我就必须为要使用的每个枚举编写一个单独的函数,而是想将枚举作为参数传递,但我很难弄清楚如何做到这一点。

这是代码:

    private enum MyColors { Red, Green, Blue }

    private Int32 GetEnumCount()
    {
        Int32 i = 0;
        while (Enum.IsDefined(typeof(MyColors), (MyColors)i))
        {
            i++;
        }
        return i;
    }

更新

我最后想出了这个答案:

    private Int32 GetEnumCount(Type enumType)
    {
        Int32 i = 0;
        while (Enum.IsDefined(enumType, i))
        {
            i++;
        }
        return i;
    }

Possible Duplicate:
Iterating through an enumeration in Silverlight?

I came up with a handy little function that gets me the count of an enum (I know it won't work properly with all enums). Rather than hard coding the Enum into the function such that I have to write a seprate function for each Enum I want to use it with, I wanted to pass the enum in as an argument but I am having difficulty figuring out how to do this.

Here is the code:

    private enum MyColors { Red, Green, Blue }

    private Int32 GetEnumCount()
    {
        Int32 i = 0;
        while (Enum.IsDefined(typeof(MyColors), (MyColors)i))
        {
            i++;
        }
        return i;
    }

UPDATE

I came up with this as the answer in the end:

    private Int32 GetEnumCount(Type enumType)
    {
        Int32 i = 0;
        while (Enum.IsDefined(enumType, i))
        {
            i++;
        }
        return i;
    }

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

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

发布评论

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

评论(3

别再吹冷风 2024-12-14 19:19:56

作为替代方案(尽管显然这已经得到解答),如果您以 0 开始第一个,然后在末尾添加一个称为 count 的值,那么您可以使用它,例如:

enum MyColour
{
     Blue = 0,
     Red,
     Green,
     ColourCount
}

As an alternative (although clearly this has already been answered) if you start the first one with 0 then add one on at the end called count then you can use that, eg:

enum MyColour
{
     Blue = 0,
     Red,
     Green,
     ColourCount
}
白昼 2024-12-14 19:19:56

这是可以完成的,但是需要一些额外的工作才能使其与不常用的枚举一起使用:

private static ulong GetEnumContiguousCount(Type enumType)
{
    var underlyingType = Enum.GetUnderlyingType(enumType);
    ulong i;
    for (i = 0; Enum.IsDefined(enumType, Convert.ChangeType(i, underlyingType, null)); ++i) {}
    return i;
}

演示:http://ideone.com/塞尔吉

This can be done, but there's some additional work needed to make it work with unusal enums:

private static ulong GetEnumContiguousCount(Type enumType)
{
    var underlyingType = Enum.GetUnderlyingType(enumType);
    ulong i;
    for (i = 0; Enum.IsDefined(enumType, Convert.ChangeType(i, underlyingType, null)); ++i) {}
    return i;
}

Demo: http://ideone.com/Serji

不醒的梦 2024-12-14 19:19:56

您应该将枚举 Type 作为参数,并删除对 (MyColors) 的强制转换。

在非 Sliverlight 中,您也可以将函数替换为

Enum.GetValues(typeof(MyEnum)).Length

You should take the enum Type as an argument, and remove the cast to (MyColors).

In non-Sliverlight, you can also just replace your function with

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