简体   繁体   English

解决温莎城堡中的仿制药

[英]Resolving generics in Windsor Castle

I'm trying to resolve object which inherits template class 我正在尝试解析继承模板类的对象

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);

next i have class which is derrived from ControllerBase 接下来我有从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");
        }
    }
}

I'm trying to resolve HardwareConnectionsController by first resolving View class which implements IViewBase interface and this works fine, I'm getting instance of a View and passing it in constructor 我正在尝试通过首先解决实现IViewBase接口的View类来解决HardwareConnectionsController的问题,并且可以正常工作,我正在获取View的实例并将其传递给构造函数

but I have problem with resolving Controller class 但是我在解析Controller类时遇到问题

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

error is: 错误是:

Castle.MicroKernel.ComponentNotFoundException has been thrown No component for supporting the service PETController.ControllerBase`1[[PETController.IViewBase, PETController, Version=1.0.4988.28227, Culture=neutral, PublicKeyToken=null]] was found stack: 抛出Castle.MicroKernel.ComponentNotFoundException没有找到支持服务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

I'm a little bit confused about your sample... but generally speaking if you registered a component For(typeof(IControllerBase<IViewBase>)) you have to resolve it using same type. 我对您的示例有些困惑...但是通常来说,如果您注册了组件For(typeof(IControllerBase<IViewBase>)) ,则必须使用相同的类型来解析它。

instead of 代替

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

try 尝试

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

I'm not sure why you are registering the component using as implementation a generic type instead of the real one... that's what I might use instead 我不确定为什么要使用通用类型而不是真正的类型来注册组件...这就是我可能会使用的类型

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

Heres some stuff that might help: 这里有一些东西可能会有所帮助:

When you register try (I think this is what you're looking for): 当您注册尝试时(我认为这是您想要的):

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

And when you resolve you need to use the IControllerBase rather than ControllerBase: 解决时,您需要使用IControllerBase而不是ControllerBase:

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

If you register the service IControllerBase<> you need to resolve IControllerBase 如果注册服务IControllerBase <>,则需要解析IControllerBase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM