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

前端 未结 6 1035
栀梦
栀梦 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 10:56

    foreach (Type t in Assembly.GetCallingAssembly().GetTypes())
    {
        if (t.GetInterface("ITheInterface") != null)
        {
            ITheInterface executor = Activator.CreateInstance(t) as ITheInterface;
            executor.PerformSomething();
        }
    }
    

提交回复
热议问题