How to list Enum's members

后端 未结 4 1856
不思量自难忘°
不思量自难忘° 2021-02-04 02:36

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

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


        
4条回答
  •  孤街浪徒
    2021-02-04 02:53

    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.

提交回复
热议问题