我使用Castle.Windsor作为DI容器,我想要的是解决任何实现的具体实现。里面有很多接口和类,每个类都实现接口。在业务层,我想解决具体的实现。这是代码部分:
public static class BootStrapper
{
//buradaki yapıları iyi bilmem gerekiyor.
private static IWindsorContainer _container = null;
public static void RegisterAllInterfaces()
{
// 1. Instantiate the IWindsor container object
_container = new WindsorContainer();
// 2. Register the services and the respective components that implement them
_container.Register(
Component.For(typeof(IProductRepository)).ImplementedBy(typeof(ProductRepository))
Component.For(typeof(IShop)).ImplementedBy(typeof(Shop))
Component.For(typeof(IPayment)).ImplementedBy(typeof(Payment))
);
}
public static Service.Somut.ProductService GetDefaultProductService()
{
Service.Somut.ProductService sonuc = null;
IProductRepository retriever = null;
retriever = _container.Resolve<IProductRepository>();
sonuc = new Service.Somut.ProductService((ProductService)retriever);
return sonuc;
}
public static object GetConcreteImplementation(Type TypeToResolve)
{
var impl = _container.Resolve(TypeToResolve);
return impl;
}
}
我想在运行时获取IProductRepository。我是这样得到的:
protected void Page_Init(object sender, EventArgs e)
{
this._presenter = new ProductListPresenter(this, BootStrapper.GetDefaultProductService());
this.ddlCustomerType.SelectedIndexChanged += delegate { _presenter.Display(); };
}
我想使用GetConcreteImplementation并将接口本身作为参数。我该怎么做?提前致谢。