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

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: