通过反射获取系统中所有继承了某接口的类

使用 Linq:

 
    
var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains( typeof (ISecurity)))) .ToArray();

不使用 Linq:

 
    
public static IEnumerable < Type > GetType(Type interfaceType) { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var type in assembly.GetTypes()) { foreach (var t in type.GetInterfaces()) { if (t == interfaceType) { yield return type; break ; } } } } }

https://q.cnblogs.com/q/26333/

你可能感兴趣的:(.net)