Getting all types that implement an interface

前端 未结 17 1340
不思量自难忘°
不思量自难忘° 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:27

    There's no easy way (in terms of performance) to do what you want to do.

    Reflection works with assemblys and types mainly so you'll have to get all the types of the assembly and query them for the right interface. Here's an example:

    Assembly asm = Assembly.Load("MyAssembly");
    Type[] types = asm.GetTypes();
    Type[] result = types.where(x => x.GetInterface("IMyInterface") != null);
    

    That will get you all the types that implement the IMyInterface in the Assembly MyAssembly

提交回复
热议问题