Get all classes that implement a certain abstract class

后端 未结 1 1030
小鲜肉
小鲜肉 2021-01-20 04:32

I\'m trying to get all classes that implement a certain abstract class. I\'m trying to do that with the following code:

var type = typeof(BaseViewComponent);         


        
相关标签:
1条回答
  • 2021-01-20 04:51
    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));
    
    0 讨论(0)
提交回复
热议问题