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
  1. Home
  2. General Programming
  3. C#
  4. How to figure out whether an enum is defined in an assembly. [modified]

How to figure out whether an enum is defined in an assembly. [modified]

Scheduled Pinned Locked Moved C#
helptutorialquestionlearning
11 Posts 3 Posters 0 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.
  • _ _AnsHUMAN_
    public class SomeEnumAttribute: DescriptionAttribute
        {
            public override string Description
            {
                get
                {
                    string resFile;
                    
                    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                    foreach (Assembly assembly in assemblies)
                    {
                        Type type = assembly.GetType("/*The type defined needs to go in here*/"); //For eg: nameofdll.MyEnum
                        if (type != null)
                        {
    	         // the type passed in GetType() is defined in this assembly so get the resource file name for it
                            resFile = assembly.GetName().Name ;
                            
                            // we have the name of the assembly - break now
                            break; 
                        }
                    }
               }
           }
    }
    //We use this class from another binary in which MyEnum is defined
     public enum MyEnum
        {
            [SomeEnumAttribute("abc")]
            abc = 0,
            [SomeEnumAttribute("def")]
            def,
            [SomeEnumAttribute("ghi")]
            ghi,
            [SomeEnumAttribute("ghi1")]
            ghi1
        }
    

    What we ought to do is this: Get the fully qualified name of the enum(MyEnum) and replace it with "/*The type defined needs to go in here*/" in the snippet above. Can someone help with this. Note: This class is defined in another binary. In another words the question is can we get the enum name(type name) in which the attribute is being used.

    You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

    modified on Thursday, June 4, 2009 4:11 AM

    0 Offline
    0 Offline
    0x3c0
    wrote on last edited by
    #2

    typeof(MyEnum).FullName

    Between the idea And the reality Between the motion And the act Falls the Shadow

    _ 1 Reply Last reply
    0
    • 0 0x3c0

      typeof(MyEnum).FullName

      Between the idea And the reality Between the motion And the act Falls the Shadow

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #3

      Thanks for the reply. The problem is that this MyEnum is defined elsewhere in another binary and not in this one.

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      0 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        Thanks for the reply. The problem is that this MyEnum is defined elsewhere in another binary and not in this one.

        You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #4

        Then you need to call the code snippet I gave in that binary. If you don't have access to it, the format is namespace.MyEnum. Just make sure you know which namespace it's in An alternative is to scan the entire assembly, and wait until you get a Type which has IsEnum set, and has a Name of MyEnum. Then, note down the FullName property, and use that when you use Assembly.GetType. Don't do the scanning in your finished program though - just whip up a small testbed to give you the FullName, and use that in your actual code

        Between the idea And the reality Between the motion And the act Falls the Shadow

        _ 1 Reply Last reply
        0
        • 0 0x3c0

          Then you need to call the code snippet I gave in that binary. If you don't have access to it, the format is namespace.MyEnum. Just make sure you know which namespace it's in An alternative is to scan the entire assembly, and wait until you get a Type which has IsEnum set, and has a Name of MyEnum. Then, note down the FullName property, and use that when you use Assembly.GetType. Don't do the scanning in your finished program though - just whip up a small testbed to give you the FullName, and use that in your actual code

          Between the idea And the reality Between the motion And the act Falls the Shadow

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #5

          Just to make it clear - Are you assuming that you have the name of the Enum, in our case MyEnum, that can be searched in the assembly. The problem still remains on getting the name of the enum in the DescriptionAttribute derived class so that I can use it to locate the assembly in which it is defined. Thanks for the help... :)

          You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

          0 1 Reply Last reply
          0
          • _ _AnsHUMAN_

            Just to make it clear - Are you assuming that you have the name of the Enum, in our case MyEnum, that can be searched in the assembly. The problem still remains on getting the name of the enum in the DescriptionAttribute derived class so that I can use it to locate the assembly in which it is defined. Thanks for the help... :)

            You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #6

            I'm still assuming that you want to get the type of MyEnum. The second paragraph shows how you could retrieve the full name. If you already know the full name (including namespace - you may be able to find this out using Red Gate's .Net Reflector), then you can pass this to Assembly.GetType

            Between the idea And the reality Between the motion And the act Falls the Shadow

            _ 1 Reply Last reply
            0
            • 0 0x3c0

              I'm still assuming that you want to get the type of MyEnum. The second paragraph shows how you could retrieve the full name. If you already know the full name (including namespace - you may be able to find this out using Red Gate's .Net Reflector), then you can pass this to Assembly.GetType

              Between the idea And the reality Between the motion And the act Falls the Shadow

              _ Offline
              _ Offline
              _AnsHUMAN_
              wrote on last edited by
              #7

              But to get the type we should have the name (namespace.MyEnum), which I don't have. I want to make it available in the DescriptionAttribute derived class.

              You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

              0 1 Reply Last reply
              0
              • _ _AnsHUMAN_

                But to get the type we should have the name (namespace.MyEnum), which I don't have. I want to make it available in the DescriptionAttribute derived class.

                You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                0 Offline
                0 Offline
                0x3c0
                wrote on last edited by
                #8

                Exactly. If you open the file in .Net Reflector, then you can search for MyEnum and get the namespace from there. But why not just make Type of MyEnum available in the DescriptionAttribute derived class, instead of the name; that way you can save the time it takes to call Assembly.GetType. As long as you know how to create your own attribute which accepts a Type instantiation and has a Type property, and how to use Assembly.GetCustomAttributes, you have the Type

                Between the idea And the reality Between the motion And the act Falls the Shadow

                1 Reply Last reply
                0
                • _ _AnsHUMAN_
                  public class SomeEnumAttribute: DescriptionAttribute
                      {
                          public override string Description
                          {
                              get
                              {
                                  string resFile;
                                  
                                  Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                                  foreach (Assembly assembly in assemblies)
                                  {
                                      Type type = assembly.GetType("/*The type defined needs to go in here*/"); //For eg: nameofdll.MyEnum
                                      if (type != null)
                                      {
                  	         // the type passed in GetType() is defined in this assembly so get the resource file name for it
                                          resFile = assembly.GetName().Name ;
                                          
                                          // we have the name of the assembly - break now
                                          break; 
                                      }
                                  }
                             }
                         }
                  }
                  //We use this class from another binary in which MyEnum is defined
                   public enum MyEnum
                      {
                          [SomeEnumAttribute("abc")]
                          abc = 0,
                          [SomeEnumAttribute("def")]
                          def,
                          [SomeEnumAttribute("ghi")]
                          ghi,
                          [SomeEnumAttribute("ghi1")]
                          ghi1
                      }
                  

                  What we ought to do is this: Get the fully qualified name of the enum(MyEnum) and replace it with "/*The type defined needs to go in here*/" in the snippet above. Can someone help with this. Note: This class is defined in another binary. In another words the question is can we get the enum name(type name) in which the attribute is being used.

                  You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                  modified on Thursday, June 4, 2009 4:11 AM

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #9

                  Are you trying to write a generic attribute or one that only works with MyEnum? If it's the former, I can't see any way around passing typeof(MyEnum) to the constructor of SomeEnumAttribute.

                  Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                  _ 1 Reply Last reply
                  0
                  • S S Senthil Kumar

                    Are you trying to write a generic attribute or one that only works with MyEnum? If it's the former, I can't see any way around passing typeof(MyEnum) to the constructor of SomeEnumAttribute.

                    Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                    _ Offline
                    _ Offline
                    _AnsHUMAN_
                    wrote on last edited by
                    #10

                    there is a way though: SomeEnum[Description(typeof(MyEnum),"value")] write a constructor in SomeEnum attribute derived class and lo and behold you have the MyEnum type in the attribute class.

                    You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                    S 1 Reply Last reply
                    0
                    • _ _AnsHUMAN_

                      there is a way though: SomeEnum[Description(typeof(MyEnum),"value")] write a constructor in SomeEnum attribute derived class and lo and behold you have the MyEnum type in the attribute class.

                      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                      S Offline
                      S Offline
                      S Senthil Kumar
                      wrote on last edited by
                      #11

                      Um, isn't that what I said in my previous post?

                      I can't see any way around passing typeof(MyEnum) to the constructor of SomeEnumAttribute.

                      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                      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