static class ReflectionHelper
    {
        public static IEnumerable<T> CreateAllInstancesOf<T>()
        {
            return typeof (ReflectionHelper).Assembly.GetTypes() //获取当前类库下所有类型
                .Where(t => typeof (T).IsAssignableFrom(t)) //获取间接或直接继承t的所有类型
                .Where(t => !t.IsAbstract && t.IsClass) //获取非抽象类 排除接口继承
                .Select(t => (T) Activator.CreateInstance(t)); //创造实例,并返回结果(项目需求,可删除)
        }
    }

 

相关文章:

  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
  • 2021-06-18
  • 2021-11-22
  • 2021-09-02
  • 2022-12-23
猜你喜欢
  • 2021-09-25
  • 2022-12-23
  • 2021-12-28
  • 2021-07-03
  • 2022-12-23
  • 2022-01-07
  • 2022-12-23
相关资源
相似解决方案