ReflectFactory.cs 566 B

1234567891011121314151617181920212223
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace FactoryPattern
  7. {
  8. /// <summary>
  9. /// 反射工厂模式
  10. /// 是针对简单工厂模式的一种改进
  11. /// </summary>
  12. public static class ReflectFactory
  13. {
  14. public static AbstractCar Create(string typeName)
  15. {
  16. Type type = Type.GetType(typeName, true, true);
  17. var instance = type?.Assembly.CreateInstance(typeName) as AbstractCar;
  18. return instance;
  19. }
  20. }
  21. }