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. Get derived classes of a base class

Get derived classes of a base class

Scheduled Pinned Locked Moved C#
csharphelpquestion
9 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.
  • N Offline
    N Offline
    nmhuy
    wrote on last edited by
    #1

    Hi everybody, I am developing a library for my company and encounter a problem. In my code, I need to know and enumerate through all the derived classes of a base class. Are there any way to get the derived classes of a base class by using reflection in C#? Hope your replies, thanks. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

    J S 2 Replies Last reply
    0
    • N nmhuy

      Hi everybody, I am developing a library for my company and encounter a problem. In my code, I need to know and enumerate through all the derived classes of a base class. Are there any way to get the derived classes of a base class by using reflection in C#? Hope your replies, thanks. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      You can iterate over every type in an assembly and check IsAssignableFrom(yourBaseType)

      N 1 Reply Last reply
      0
      • J J4amieC

        You can iterate over every type in an assembly and check IsAssignableFrom(yourBaseType)

        N Offline
        N Offline
        nmhuy
        wrote on last edited by
        #3

        I got it! Thank you for you helping! Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

        1 Reply Last reply
        0
        • N nmhuy

          Hi everybody, I am developing a library for my company and encounter a problem. In my code, I need to know and enumerate through all the derived classes of a base class. Are there any way to get the derived classes of a base class by using reflection in C#? Hope your replies, thanks. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

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

          Can we know why you do this? It's usually a bad idea for code to know derived classes from base classes. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          N 1 Reply Last reply
          0
          • S S Senthil Kumar

            Can we know why you do this? It's usually a bad idea for code to know derived classes from base classes. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            N Offline
            N Offline
            nmhuy
            wrote on last edited by
            #5

            Hi, Here is the context that I use reflection: - The library has a base class, e.g AbstractAlgorithm, and some derived classes, e.g ConcreteAlgorithm1, ConcreteAlgorithm2, ... We can get an algorithm instance like this: AbstractAlgorithm algo = new ConcreateAlgorithm1(); - I want to provide another way for client to get an algorithm instance by its name like this: AbstractAlgorithm algo = AlgorithmFactory.createInstance("XYZ algorithm"); So I add an abstract method "getName()" to AbstractAlgorithm base class and each concreate algorithm class implements this method to return its name. The "createInstance(string)" method of AlgorithmFactory will use reflection to enumerate through the derived classes of AbstractAlgorithm, call their "getName()" methods to select the right algorithm class. After that, I use .NET class Activator to create an algorithm instance of the selected class. By doing this (instead of using "switch case" statement), the "createInstance(string)" method is unchanged when new concrete algorithm classes are added to the library. Is it a right design? Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

            S 1 Reply Last reply
            0
            • N nmhuy

              Hi, Here is the context that I use reflection: - The library has a base class, e.g AbstractAlgorithm, and some derived classes, e.g ConcreteAlgorithm1, ConcreteAlgorithm2, ... We can get an algorithm instance like this: AbstractAlgorithm algo = new ConcreateAlgorithm1(); - I want to provide another way for client to get an algorithm instance by its name like this: AbstractAlgorithm algo = AlgorithmFactory.createInstance("XYZ algorithm"); So I add an abstract method "getName()" to AbstractAlgorithm base class and each concreate algorithm class implements this method to return its name. The "createInstance(string)" method of AlgorithmFactory will use reflection to enumerate through the derived classes of AbstractAlgorithm, call their "getName()" methods to select the right algorithm class. After that, I use .NET class Activator to create an algorithm instance of the selected class. By doing this (instead of using "switch case" statement), the "createInstance(string)" method is unchanged when new concrete algorithm classes are added to the library. Is it a right design? Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

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

              How about simply passing the type of the derived class, instead of a string representing the type? Something like

              AlgorithmFactory.CreateInstance(typeof(ConcreteAlgorithm1));

              Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              N 1 Reply Last reply
              0
              • S S Senthil Kumar

                How about simply passing the type of the derived class, instead of a string representing the type? Something like

                AlgorithmFactory.CreateInstance(typeof(ConcreteAlgorithm1));

                Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                N Offline
                N Offline
                nmhuy
                wrote on last edited by
                #7

                I think client will prefer short and friendly name to long and hard to remember class name. I just study it from the Java Cryptography Framework. We can get an MD5 hash algorithm like this: MessageDigest md = MessageDigest.getInstance("MD5"); We do not need to know the name of the class that implements the MD5 hash algorithm. Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

                S 1 Reply Last reply
                0
                • N nmhuy

                  I think client will prefer short and friendly name to long and hard to remember class name. I just study it from the Java Cryptography Framework. We can get an MD5 hash algorithm like this: MessageDigest md = MessageDigest.getInstance("MD5"); We do not need to know the name of the class that implements the MD5 hash algorithm. Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

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

                  Yeah, it's a tradeoff :- with type names, you get compile time errors if the user passes an invalid type, with strings, you don't. OTOH, like you said, it's more convenient for the users.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                  N 1 Reply Last reply
                  0
                  • S S Senthil Kumar

                    Yeah, it's a tradeoff :- with type names, you get compile time errors if the user passes an invalid type, with strings, you don't. OTOH, like you said, it's more convenient for the users.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    N Offline
                    N Offline
                    nmhuy
                    wrote on last edited by
                    #9

                    Yes, it's a trade-off. Thanks for sharing your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi

                    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