Dynamic access of classes of a given namespace

Posted by user322383 on Stack Overflow See other posts from Stack Overflow or by user322383
Published on 2010-05-23T01:51:29Z Indexed on 2010/05/23 2:00 UTC
Read the original article Hit count: 281

Filed under:
|
|

Hi!

I'm writing an interface that will be implemented by a lot of classes, and I'm writing a class that will hold a collection of instances of these implementations. Every class will have a default constructor.

So, is there a simple way (e.g. using some kind of reflection) to put an instance of each of these implementing classes to the collection? Besides doing it manually, which is simple, yes, but a lot of work and error prone (what if I missed an implementation while writing the method? What if a new implementation came and I forgot to update the given method?).

So, what I would like is to be able to iterate through all classes of a given namespace or maybe through the list of all available classes. My method then would simply check, through reflection, if the given class implements the given interface, and if it does, puts it into the collection.

Thank you.

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection