TOP

C#/.Net 获取子类和实现接口的类

- 自创 | 点击量:
  • 获取一个Assembly下的子类

var bases = Assembly    
            .GetExecutingAssembly()    
            .GetTypes()    
            .Where(item =>item.IsSubclassOf(typeof(Base)))        
            .Select(type =>(Base)Activator.CreateInstance(type))
            .ToList();
  • 获取所有Assembly下的子类

var bases = AppDomain    
            .CurrentDomain
            .GetAssemblies()    
            .SelectMany(item =>item.GetTypes())
            .Where(item =>item.IsSubclassOf(typeof(Base)))
            .Select(type =>(Base)Activator.CreateInstance(type))
            .ToList();
  • 获取一个Assembly下的实现接口的类

var interfaceImplements = Assembly    
                          .GetExecutingAssembly()    
                          .GetTypes()    
                          .Where(item =>item.GetInterfaces().Contains(typeof(Interface)))
                          .Select(type =>(Interface)Activator.CreateInstance(type))
                          .ToList();
  • 获取所有Assembly下的实现接口的类

var interfaceImplements = AppDomain    
                         .CurrentDomain
                         .GetAssemblies()
                         .SelectMany(item =>item.GetTypes())
                         .Where(item =>item.GetInterfaces().Contains(typeof(Interface)))
                         .Select(type =>(Interface)Activator.CreateInstance(type))
                         .ToList();

转载注明(米茨-MICZ)原文地址:

相关文章!