关于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();
         }
     }
}
------解决方案--------------------
C# code
namespace BLL
{
  public interface ICar
  {
  void Say();
  }
}