首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于Activator.CreateInstance解决方案

2012-10-16 
关于Activator.CreateInstance学习三层结构,想用反射,结果单个文件里可以实现,拆成三个项目就出错,在web和

关于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();  }} 

热点排行