如何将对象传递给windsor容器而不是类型?

时间:2008-09-28 06:39:39

标签: c# .net dependency-injection inversion-of-control castle-windsor

我有以下课程:

public class ViewPage<TView,TPresenter> : Page 
    where TView : IView
    where TPresenter : Presenter<TView>
{
    public ViewPage()
    {
        if (!(this is TView))
            throw new Exception(String.Format("The view must be of type {0}", typeof(TView)));

        IWindsorContainer container = new WindsorContainer();
        container.AddComponent("view", typeof(IView), typeof(TView));
        container.AddComponent("presenter", typeof(Presenter<TView>), typeof(TPresenter));
        TPresenter presenter = container[typeof(TPresenter)] as TPresenter;
    }
}

这是Presenter代码:

public class Presenter<T> where T : IView
{
    public T View { get; private set; }

    public Presenter(T view)
    {
        this.View = view;
    }
}

我想通过Windsor将当前的ViewPage实例传递给TPresenter,而不是让它实例化一个新对象。这可能吗?

0 个答案:

没有答案