求助:关于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);这一句错误了,可是不知道怎么写?

posted @ 2012-09-27 10:45  做个好男人  阅读(372)  评论(1编辑  收藏  举报