vb.net 如何列出 Enum 的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6269218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:32:06  来源:igfitidea点击:

How to list Enum's members

vb.netenums

提问by user774411

How to list Enum's members in code? I have the following Enum:

如何在代码中列出 Enum 的成员?我有以下枚举:

Public Enum TestEnum As int32
    First = 0
    Second = 2
    Third = 4
    Fourth = 6
End Enum

And I try to list all members of TestEnum via the following code but it failed:

我尝试通过以下代码列出 TestEnum 的所有成员,但失败了:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Enum1 As TestEnum

        Dim Members() As String

        Members = System.Enum.GetNames(CType(Enum1, System.Enum))


    End Sub
End Class

So, my question is: How to list members of an Enum?

所以,我的问题是:如何列出枚举的成员?

Update: The solution is:

更新:解决方案是:

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Members() As String
        Members = System.Enum.GetNames(GetType(TestEnum))

        MessageBox.Show(Join(Members, Chr(13) & Chr(10)))

    End Sub
End Class

回答by George Filippakos

You can simply iterate through all values like this:

您可以简单地遍历所有值,如下所示:

Public Enum TestEnum As int32
    First = 0
    Second = 2
    Third = 4
    Fourth = 6
End Enum

For Each tstEnum As TestEnum In System.Enum.GetValues(GetType(TestEnum))

    Response.Write(
        String.Format("Name: {0}  Value: {1}", 
            tstEnum.ToString, 
            CInt(tstEnum).ToString
        )
    )

Next

回答by Sven

You need to pass a type, not a value, to the method.

您需要将类型而不是值传递给该方法。

Members = System.Enum.GetNames(GetType(TestEnum))

If you have an instance of your enum you can also use

如果您有枚举的实例,也可以使用

Members = System.Enum.GetNames(Enum1.GetType())

Though I would recommend the first approach if you know the type you want.

如果你知道你想要的类型,我会推荐第一种方法。

回答by Brandon Moretz

Have you looked at Enum.GetValues?

你看过Enum.GetValues吗?

Edit: To clarify, yes you need to pass a Type not an instance of the enum to either method.

编辑:澄清一下,是的,您需要将类型而不是枚举的实例传递给任一方法。

Enum.GetNames(GetType(TestEnum))

回答by Paul Karam

I have used George Filippakos answersince I wanted to know how to iterate through Enumvalues.

因为我想知道如何迭代值,所以我使用了George Filippakos 的答案Enum

I also found out that you can do it using Type.GetEnumValueswhich has been available since .NET Framework 4.0.

我也发现了,你可以用做Type.GetEnumValues因为这已面世.NET Framework 4.0

Here's the two ways you can use to iterate through EnumValues:

以下是您可以用来遍历Enum值的两种方法:

Module Module1
    Sub Main()
        For Each tstEnum As TestEnum In System.Enum.GetValues(GetType(TestEnum))
            Console.WriteLine($"Name: {tstEnum.ToString}, Value: {CType(tstEnum, Integer)}")
        Next

        Console.WriteLine(Environment.NewLine)

        For Each tstEnum As TestEnum In GetType(TestEnum).GetEnumValues
            Console.WriteLine($"Name: {tstEnum.ToString}, Value: {CType(tstEnum, Integer)}")
        Next

        Console.ReadKey()
    End Sub

    Public Enum TestEnum
        First = 1
        Second = 2
        Third = 3
    End Enum
End Module


Output:

输出:

Name: First, Value: 1
Name: Second, Value: 2
Name: Third, Value: 3

Name: First, Value: 1
Name: Second, Value: 2
Name: Third, Value: 3