zoukankan      html  css  js  c++  java
  • 求助:关于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);这一句错误了,可是不知道怎么写?

  • 相关阅读:
    数码管按键加减一
    单片机软件proteus的汉化步骤
    不同位数数字取个十百千位数字的代码
    直升机基础知识
    数码管应用digital_pile
    proteus中的常用文件
    蜂鸣器类代码
    延时函数sys
    求数组最大子数组
    Python数据结构与算法
  • 原文地址:https://www.cnblogs.com/shangxia/p/2705219.html
Copyright © 2011-2022 走看看