Monday, June 10, 2013

Get classes based on Interface with Linq

This weekend I was searching for an easy way to get all classes which implement an given interface.
After searching I found an easy way to get the classes by given type. Using Linq getting the following code:
var type = typeof(IMyInterface);
var types = AppDomain.CurrentDomain.GetAssemblies()
.ToList()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface);

No comments:

Post a Comment