Getting all types that implement an interface

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

    This worked for me. It loops though the classes and checks to see if they are derrived from myInterface

     foreach (Type mytype in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                     .Where(mytype => mytype .GetInterfaces().Contains(typeof(myInterface)))) {
        //do stuff
     }
    

提交回复
热议问题