Getting all types that implement an interface

前端 未结 17 1033
不思量自难忘°
不思量自难忘° 2020-11-22 00:34

Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations?

This is what I want to re-

17条回答
  •  走了就别回头了
    2020-11-22 01:22

    loop through all loaded assemblies, loop through all their types, and check if they implement the interface.

    something like:

    Type ti = typeof(IYourInterface);
    foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) {
        foreach (Type t in asm.GetTypes()) {
            if (ti.IsAssignableFrom(t)) {
                // here's your type in t
            }
        }
    }
    

提交回复
热议问题