C#找出接口的所有实现类并遍历执行这些类的公共方法

来源:互联网 发布:举而不坚,坚而不久知乎 编辑:程序博客网 时间:2024/06/05 04:26

先找出继承自该接口的接口和实现类,然后遍历这些类型,实例化实现类,最后执行接口的方法。

 var type = typeof(IConfigRepository);            var types = AppDomain.CurrentDomain.GetAssemblies()                .SelectMany(s => s.GetTypes())                .Where(p => type.IsAssignableFrom(p.GetGenericTypeDefinition()));            foreach (var v in types)            {                if (v.IsClass)                {                    (Activator.CreateInstance(v) as IConfigRepository).RemoveByConfigVersionID(ConfigVersionID,EnvType);                 }            }
阅读全文
0 0