Get all classes that implement a specific abstract class
I am trying to get all classes that implement a specific abstract class. I am trying to do it with the following code:
var type = typeof(BaseViewComponent);
var types = Assembly
.GetEntryAssembly()
.GetReferencedAssemblies()
.Select(Assembly.Load)
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p));
But for now I can only get the abstract class. Not any class that implements this base class.
What do I need to change to get all classes that implement this abstract base class?
+3
source to share
1 answer
using System.Reflection;
using Microsoft.Extensions.DependencyModel;
var asmNames = DependencyContext.Default.GetDefaultAssemblyNames();
var type = typeof(BaseViewComponent);
var allTypes = asmNames.Select(Assembly.Load)
.SelectMany(t => t.GetTypes())
.Where(p => p.GetTypeInfo().IsSubclassOf(type));
+4
source to share