Get classes that implement an interface

Here’s a convenient piece of code that you can use to get all classes that implement a given interface. Why do you need this? I assume that you have been programming against an interface rather than against a concrete implementation. Which is cool. Now since the interface is the common factor, here’s a way to get your objects of that type

var type = typeof(IMyInterface);

var types = AppDomain.CurrentDomain.GetAssemblies()

     .SelectMany(s => s.GetTypes())

     .Where(p => type.IsAssignableFrom(p));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s