How to find derived classes?

  • Thread starter Thread starter Zark3
  • Start date Start date
Z

Zark3

Hello all,

Is there a way to find all classes that are derived from a specific
base type?
Within the namespace of my project i define a base type that may be
inherited for future expansion, so when checking for this class i need
to know all specific implemented types.

Right now I have a code snippet like:
Type[] types = new Type[] { typeof(FileData),
typeof(FileData_Image) };
foreach (Type t in types) {
(...)

What I would ideally like to have is something like:
Type[] types =
typeof(myBaseType).GetAllClassesThatDeriveFromThisClass("withinMyNamespace");
foreach (Type t in types) {
(...)

Is this possible?

Chris
 
Zark3 said:
Is there a way to find all classes that are derived from a specific
base type?

Yes, but not in a one-liner. Basically, for each assembly you're
interested in, you need to look through all the types, and then check
each type with IsSubclassOf.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top