• 求助:关于Activator.CreateInstance


    学习三层结构,想用反射,结果单个文件里可以实现,拆成三个项目就出错,在web和DAL两个项目里都添加了BLL引用,下面是代码,还请多多指教!



    namespace BLL
    {
      public class CarFactory
      {
      public static ICar BuildCar()
      {
      BLL.ICar myCar = null;
      try
      {
      Type type = Type.GetType("DAL.Bus", true);
      myCar = (ICar)Activator.CreateInstance(type);
      }
      catch (TypeLoadException e)
      {
      Console.WriteLine("Unknow Car. Exception: - {0}", e.Message);
      }
      return myCar;
      }
      public static void ui()
      {
      BLL.ICar myCar = BLL.CarFactory.BuildCar();
      myCar.Say();
      }
      }

    }
    namespace BLL
    {
      public interface ICar
      {
      void Say();
      }
    }
    namespace DAL
    {
      public class Bus : BLL.ICar
      {
      public void Say()
      {
      Console.WriteLine("I am a Bus");
      }
      }
    }
    namespace Web
    {
      public partial class _Default : System.Web.UI.Page
      {
      protected void Page_Load(object sender, EventArgs e)
      {
      BLL.CarFactory.ui();
      }
      }
    }

    应该是Type type = Type.GetType("DAL.Bus", true);这一句错误了,可是不知道怎么写?

  • 相关阅读:
    延迟加载和缓存
    动态SQL
    Mybatis框架模糊查询+多条件查询
    mybatis增删改
    初始mybatis(二)
    Struts2框架和SpringMvc框架的区别
    MyBatis框架与Hibernate 框架的区别
    初始mybatis
    Servlet
    find命令使用
  • 原文地址:https://www.cnblogs.com/shangxia/p/2705219.html
Copyright © 2020-2023  润新知