Getting all types from an assembly derived from a base class

I try to check the contents of the assembly and find in it all the classes that are directly or indirectly obtained from Windows.Forms.UserControl.

I'm doing it:

Assembly dll = Assembly.LoadFrom(filename); var types = dll.GetTypes().Where(x => x.BaseType == typeof(UserControl)); 

But it gives an empty list, because none of the classes directly extends UserControl. I don’t have enough understanding to do it quickly, and I would prefer not to write a recursive function if I don’t need it.

+6
reflection c #
source share
2 answers

Instead, you should use Type.IsSubclassOf :

 var types = dll.GetTypes().Where(x => x.IsSubclassOf(typeof(UserControl))); 
+17
source share

You can use:

  var assembly = Assembly.Load(filename); var types = assembly.GetTypes().Where((type) => typeof(UserControl).IsAssignableFrom(type)); 
+1
source share

All Articles