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 get the names of all classes wich are derived from a base class ?

How to get the names of all classes wich are derived from a base class ?

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 4 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.
  • M Offline
    M Offline
    MarkPhB
    wrote on last edited by
    #1

    Hi does anyone know how i can get the names of all available classes wich are derived from a base class ?

    J S 2 Replies Last reply
    0
    • M MarkPhB

      Hi does anyone know how i can get the names of all available classes wich are derived from a base class ?

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Here's how to get all derived classes of a base class in one assembly:

      List<Type> derived = new List<Type>();
      Type baseClassType = typeof(BaseClass);
      foreach(Type t in baseClassType.Assembly.GetTypes())
      {
         if(t.IsSubclassOf(baseClassType))
         {
              derived.Add(t);
         }
      }

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord Is So Good The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      1 Reply Last reply
      0
      • M MarkPhB

        Hi does anyone know how i can get the names of all available classes wich are derived from a base class ?

        S Offline
        S Offline
        ss20
        wrote on last edited by
        #3

        With this code you can get all derived classes from the current assembly and its referenced assemblies... (this should be all assemblies in the project)

        List<Type> derives = new List<Type>();
        foreach( AssemblyName name in Assembly.GetExecutingAssembly().GetReferencedAssemblies() ) {
          Assembly assembly = Assembly.Load( name );
        
          foreach( Type class in assembly.GetTypes() ) {
            if( class.IsSubclassOf( typeof( BaseClass ) ) ) {
              derives.Add( class );
            }
          }
        }
        
        S 1 Reply Last reply
        0
        • S ss20

          With this code you can get all derived classes from the current assembly and its referenced assemblies... (this should be all assemblies in the project)

          List<Type> derives = new List<Type>();
          foreach( AssemblyName name in Assembly.GetExecutingAssembly().GetReferencedAssemblies() ) {
            Assembly assembly = Assembly.Load( name );
          
            foreach( Type class in assembly.GetTypes() ) {
              if( class.IsSubclassOf( typeof( BaseClass ) ) ) {
                derives.Add( class );
              }
            }
          }
          
          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          It will be all referenced assemblies, but not necessarily all assemblies in the project. Referenced assemblies are those that are actually listed in the References tree in Visual Studio and imported by namespace into the project. If you have any late bound assemblies (assemblies loaded by reflection using Assembly.Load()), they won't be in this list.

          Scott.


          —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

          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