LINQ: List Classes implementing the IEnumerable Interface

A reader ‘Richard’ mailed me with a question. He wanted to know an easy way to find all the types that implement the IEnumerable interface, or for that matter, any interface. Here’s a piece of LINQ code that lists all the types implementing the IEnumerable interface

public static void Main(string[] args) 
   var t = typeof(IEnumerable); 

   var typesIEnum = AppDomain.CurrentDomain
  .SelectMany(x => x.GetTypes())
  .Where(x => t.IsAssignableFrom(x)); 

foreach (var types in typesIEnum) 

Here’s a partial output

types implementing interface

Note: The results may not be the same on your machine. Note that we are referring to the GetExportedTypes() which returns type visible outside the assembly. So if you have added new references (assemblies) to your project which implements the IEnumerable or changed the access modifiers of your custom types, you will get different results.

Liked this tip? Read some more LINQ Tips

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

No comments: