How to find all the classes which implement a given interface?

前端 未结 6 1038
栀梦
栀梦 2020-11-27 10:36

Under a given namespace, I have a set of classes which implement an interface. Let\'s call it ISomething. I have another class (let\'s call it CClass

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 11:03

    Maybe we should go this way

    foreach ( var instance in Assembly.GetExecutingAssembly().GetTypes().Where(a => a.GetConstructor(Type.EmptyTypes) != null).Select(Activator.CreateInstance).OfType() ) 
       instance.Execute(); 
    

提交回复
热议问题