0

我正在尝试解析继承模板类的对象

using System;
using Controller;

namespace Controller
{
    public interface IControllerBase<T> where T:IViewBase
    {

    }

    public class ControllerBase
    {
        public string ModuleName
        {
            get;
            set;
        }
    }

    public class ControllerBase<T> : ControllerBase, IControllerBase<T>
        where T : IViewBase
    {

        public virtual T View {
            get;
            set;
        }

        public ControllerBase (T instance)
        {
            View = instance;
        }
    }

    }



IControllerBase was added recently, just to match this strategy:

    container.Register (Component.For(typeof(IControllerBase<IViewBase>)).ImplementedBy(typeof(ControllerBase<IViewBase>)).LifeStyle.Transient);

接下来我有从 ControllerBase 派生的类

using System;

namespace HardwareConnections
{
    public class HardwareConnectionsController : Controller.ControllerBase<IHardwareConnections>
    {
        public HardwareConnectionsController(IHardwareConnections view) : base(view)
        {
            base.ModuleName = "Hardware connections";
            Console.WriteLine("\n\n\n\n");
            Console.WriteLine("Constructor fired");
            Console.WriteLine("\n\n\n\n");
        }
    }
}

我正在尝试通过首先解析实现 IViewBase 接口的 View 类来解析 HardwareConnectionsController,这工作正常,我正在获取 View 的实例并将其传递给构造函数

但我在解析控制器类时遇到问题

var hcon = container.Resolve<ControllerBase<IViewBase>>(new { view = plug});

错误是:

Castle.MicroKernel.ComponentNotFoundException has been throwed No component for support the service PETController.ControllerBase`1[[PETController.IViewBase, PETController, Version=1.0.4988.28227, Culture=neutral, PublicKeyToken=null]] 堆栈:

Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service PETController.ControllerBase`1[[PETController.IViewBase, PETController, Version=1.0.4988.28227, Culture=neutral, PublicKeyToken=null]] was found
  at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve (System.Type service, IDictionary arguments, IReleasePolicy policy) [0x00000] in <filename unknown>:0
  at Castle.MicroKernel.DefaultKernel.Resolve (System.Type service, IDictionary arguments) [0x00000] in <filename unknown>:0
  at Castle.Windsor.WindsorContainer.Resolve[ControllerBase`1] (System.Object argumentsAsAnonymousType) [0x00000] in <filename unknown>:0
  at (wrapper remoting-invoke-with-check) Castle.Windsor.WindsorContainer:Resolve (object)
  at PETController.PluginFactory.LoadPlugins[IViewBase] (System.String searchLocation) [0x001f6] in /home/konrad/hg/PET/PET/PETController/Utils/PluginFactory.cs:67
  at MainWindow..ctor () [0x00034] in /home/konrad/hg/PET/PET/PETController/MainWindow.cs:19
  at PETController.MainClass.Main (System.String[] args) [0x00005] in /home/konrad/hg/PET/PET/PETController/Main.cs:11


enter code here
4

2 回答 2

1

我对你的示例有点困惑......但一般来说,如果你注册了一个组件For(typeof(IControllerBase<IViewBase>)),你必须使用相同的类型来解决它。

代替

 var hcon = container.Resolve<ControllerBase<IViewBase>>(new { view = plug});

尝试

var hcon = container.Resolve<IControllerBase<IViewBase>>(new { view = plug});

我不确定您为什么要使用泛型类型而不是真实类型作为实现来注册组件......这就是我可能使用的

container.Register (Component.For(typeof(IControllerBase<IViewBase>)).ImplementedBy(typeof(HardwareConnectionsController)).LifeStyle.Transient);
于 2013-09-03T08:31:16.867 回答
0

这里有一些可能有帮助的东西:

当您注册尝试时(我认为这就是您要寻找的):

container.Register (Component.For(typeof(IControllerBase<>)).ImplementedBy(typeof(ControllerBase<>)).LifeStyle.Transient);

当您解决时,您需要使用 IControllerBase 而不是 ControllerBase:

var hcon = container.Resolve<IControllerBase<IViewBase>>(new { view = plug});

如果您注册服务 IControllerBase<> 您需要解析 IControllerBase

于 2013-09-03T08:37:06.943 回答