Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to get the Enum item names using Reflection

How to get the Enum item names using Reflection

Scheduled Pinned Locked Moved C#
csharpc++tutorial
13 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mcljava

    You can also simply do a ToString(). For example: string s = tenumArtMode.arModeManual.ToString(); Would yield the string "artModeManual" Good Luck Mike Luster CTI/IVR/Telephony SME

    E Offline
    E Offline
    engsrini
    wrote on last edited by
    #3

    Thanks For your reply Mike, No, This can't be applicable, b'cas i getting this enum at runtime so i would not know the enum item names. Do understand my question. I want to iterate the enum using reflections Regards, Srini

    G C 2 Replies Last reply
    0
    • E engsrini

      Thanks For your reply Mike, No, This can't be applicable, b'cas i getting this enum at runtime so i would not know the enum item names. Do understand my question. I want to iterate the enum using reflections Regards, Srini

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #4

      You don't need reflection. The Enum class provides you with methods to get all information you nead about the enum. Use the GetNames method to get an array of all the names. If you need their values, use the GetValues method to get an array with all the values. --- b { font-weight: normal; }

      E 1 Reply Last reply
      0
      • G Guffa

        You don't need reflection. The Enum class provides you with methods to get all information you nead about the enum. Use the GetNames method to get an array of all the names. If you need their values, use the GetValues method to get an array with all the values. --- b { font-weight: normal; }

        E Offline
        E Offline
        engsrini
        wrote on last edited by
        #5

        If it is One Enum then your suggestions is OK. I can use Enum.GetNames(enum), But My situation is likes a structure which will contain lot of enums(say 100) as it member) so i can't write 100's lines of code to display every member ie. enum1 { a=0, b, c } .. enum100 { x=0, y, z } Struct st { enum1 e1; enum2 e2; .. enum100 e100; }; Enum.GetNames(st.e1); .. Enum.GetNames(st.e100); Instead Puting like the above I am thinking of reflection to reduce the line of code Like this Type ty = st.GetType(); FieldInfo[] fin= ty.GetFields(); foreach (FieldInfo fino in fin) { fino.GetValue(st).ToString(); } But This print the enum1..enum100 values like 0 0 0 0 .. but I want to print the enum member names like a b c ... Hope I Explained my question Clearyly Regards, Srini

        G 1 Reply Last reply
        0
        • E engsrini

          If it is One Enum then your suggestions is OK. I can use Enum.GetNames(enum), But My situation is likes a structure which will contain lot of enums(say 100) as it member) so i can't write 100's lines of code to display every member ie. enum1 { a=0, b, c } .. enum100 { x=0, y, z } Struct st { enum1 e1; enum2 e2; .. enum100 e100; }; Enum.GetNames(st.e1); .. Enum.GetNames(st.e100); Instead Puting like the above I am thinking of reflection to reduce the line of code Like this Type ty = st.GetType(); FieldInfo[] fin= ty.GetFields(); foreach (FieldInfo fino in fin) { fino.GetValue(st).ToString(); } But This print the enum1..enum100 values like 0 0 0 0 .. but I want to print the enum member names like a b c ... Hope I Explained my question Clearyly Regards, Srini

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #6

          I see. Try this, then: Enum.GetName(fino.FieldType, fino.GetValue(st)) --- b { font-weight: normal; }

          E 1 Reply Last reply
          0
          • G Guffa

            I see. Try this, then: Enum.GetName(fino.FieldType, fino.GetValue(st)) --- b { font-weight: normal; }

            E Offline
            E Offline
            engsrini
            wrote on last edited by
            #7

            Exactly thats what i want, i have used the same code but it throws me me ArgumentException, i think since the enum is developed in VC++ .net fino.FieldType doesn't match with System.Enum Type in C#(since we are using Enum.GetName).. I think so. Try it out and if anything works please share with me. Regards, Srini

            G 1 Reply Last reply
            0
            • E engsrini

              Exactly thats what i want, i have used the same code but it throws me me ArgumentException, i think since the enum is developed in VC++ .net fino.FieldType doesn't match with System.Enum Type in C#(since we are using Enum.GetName).. I think so. Try it out and if anything works please share with me. Regards, Srini

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #8

              Have you tried it out? It's not the same code as you showed in your first post. --- b { font-weight: normal; }

              E 1 Reply Last reply
              0
              • G Guffa

                Have you tried it out? It's not the same code as you showed in your first post. --- b { font-weight: normal; }

                E Offline
                E Offline
                engsrini
                wrote on last edited by
                #9

                Yes I tried out. it throws me the ArgumentException. ArgumentException - Description given in the MSDN help enumType is not an Enum. -or- value is neither of type enumType nor does it have the same underlying type as enumType. Enum in VC++ .net and VC# .net are they different?

                E 1 Reply Last reply
                0
                • E engsrini

                  Yes I tried out. it throws me the ArgumentException. ArgumentException - Description given in the MSDN help enumType is not an Enum. -or- value is neither of type enumType nor does it have the same underlying type as enumType. Enum in VC++ .net and VC# .net are they different?

                  E Offline
                  E Offline
                  engsrini
                  wrote on last edited by
                  #10

                  Even That is code i have mentioned in my initial Conversation.

                  G 1 Reply Last reply
                  0
                  • E engsrini

                    Even That is code i have mentioned in my initial Conversation.

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #11

                    No, it's not. There you used a Type object instead of a FieldInfo object. --- b { font-weight: normal; }

                    1 Reply Last reply
                    0
                    • E engsrini

                      Thanks For your reply Mike, No, This can't be applicable, b'cas i getting this enum at runtime so i would not know the enum item names. Do understand my question. I want to iterate the enum using reflections Regards, Srini

                      C Offline
                      C Offline
                      chriswa
                      wrote on last edited by
                      #12

                      Have you tried using the Enum struct? Once you have the type of enum you can use the static methods of the Enum struct to obtain information about the enum type.:suss: chriswa

                      E 1 Reply Last reply
                      0
                      • C chriswa

                        Have you tried using the Enum struct? Once you have the type of enum you can use the static methods of the Enum struct to obtain information about the enum type.:suss: chriswa

                        E Offline
                        E Offline
                        engsrini
                        wrote on last edited by
                        #13

                        I got it!!!! Thanks Chriswa its working as per your suggestion. i have declared the enum as enum class (ie enum struct) in VC++ dll. then i can able to get the enum member names. If i declare the enum using enum in VC++, then i am not able to retrieve the names. What is the difference between enum and enum struct I extend my thanks to Guffa. Regards,:-D Srini

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups