Getting a list of derived class types of a base class

As an abstraction of my problem, I have a base class of type cAnimal and then an unknown number of derived classes, e.g cMammal, cBird, cReptile amongst others.  
How can I get a list of the derived classes of CAnimal by specifying CAnimal.  I am not after a list of instantiated animals or a collectionbase example, rather I would like a list of the derived classes only.
A VB answer is preferred.
Thank you!
dgloverukAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
To look for classes in the executing assembly, you can use something like the following (which you can of course extend to load other assemblies if required):
Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

For Each t As Type In asm.GetTypes()
    If t.IsSubclassOf(GetType(cAnimal)) Then
        Console.WriteLine(t.Name + " inherits from cAnimal")
    End If
Next

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dgloverukAuthor Commented:
That's cool, thank you Carl.  If where you console.writeline the class name instead I wanted to instantiate a class of the type and return a property is that a lot more complicated?
Carl TawnSystems and Integration DeveloperCommented:
Not that much more complicated. It can vary slightly depending on whether or not the property is defined in the base class or not.

For this example i'm assuming the derived classes all have a property called "Name", that isn't defined in the base class:
        Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()

        For Each t As Type In asm.GetTypes()
            If t.IsSubclassOf(GetType(cAnimal)) Then

                Dim o As Object = Activator.CreateInstance(t)
                Dim value As String = o.GetType().InvokeMember("Name", Reflection.BindingFlags.GetProperty, Nothing, o, Nothing)

                Console.WriteLine("Property value: " & value)
            End If
        Next

Open in new window

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

dgloverukAuthor Commented:
Cool and if wanted to declare an object of the encountered derived class type e.g something like
dim DerivedAnimalClass as t
could I do this?
Thanks!
Carl TawnSystems and Integration DeveloperCommented:
You can assign to a variable typed to the base class if you wanted to.
Something like:
Dim o As cAnimal = CType(Activator.CreateInstance(t), cAnimal)

Open in new window

dgloverukAuthor Commented:
Thank you Carl, especially for so perfectly addressing the follow up questions!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.