开发者

castle windsor register a generic interface and retrieve all registered instances the inherit indirectly from it

开发者 https://www.devze.com 2023-01-19 23:03 出处:网络
I have the following structure - public interface IBaseInterface<T> { } public interface IChildInterface1<Class1> : IBaseInterface<Class1>

I have the following structure -

public interface IBaseInterface<T>
{
}

public interface IChildInterface1<Class1> : IBaseInterface<Class1>
{
}

public interface IChildInterface2<Class2> : IBaseInterface<Class2>
{
}

public class ImplementationClass1 : IChildInterface1<Class1>
{
}

public class ImplementationClass2 : IChildInterface2<Class2>
{
}

I want to register all types in the assembly that implement IBaseInterface directly or indirectly, so I want to be able to register ImplementationClass1 and ImplementationClass2 given IBaseInterface. And then I want to retrieve any concrete type based on the generic type definition of ChildInterface classes.

I am registering types like this -

container.Register(
    AllTypes.FromAssemblyContaining<Class1>()
            .BasedOn(typeof(IBaseInterface<>))
            .WithService.Base()
            .Configure(c => c.LifeStyle.Transient)
     );

and then resolving the type using

var impl = container.Resolve(typeof(IChildInterface1<>))

I expected this to give me an instance of ImplementationClass1, however I get the error -

No component for supporting the service IChildInterface`1 was found

Can someone figure out what I am doing wrong, is this even a valid scenario that I am attempting?

This is my whole code -

using Castle.MicroKernel.Registration;

namespace complexresolutionioc
{
  class Program
  {
    static void Main(string[] args)
    {
      var container = new Castle.Windsor.WindsorContainer();

      container.Register(
        AllTypes.FromAssemblyContaining<Class1>()
              .BasedOn(typeof(IBaseInterface<>))
              .WithService.AllInterfaces()
              .Configure(c => c.LifeStyle.Transient)
     );

      var thisWorks = container.Resolve<IChildInterface1<Class2>>();
    }
  }

  public interface IBaseInterface<T>
  {
  }

  public interface IChildInterface1<Class1> : IBaseInterface<Class1>
  {
  }

  public interface IChildInterface2<Class2> : IBaseInterface<Class2>
  {
  }

  public class ImplementationClass1 : IChildInterface1<Class1>
  {
  }

  public class ImplementationClass2 : IChildInterface2<Class2>
  {
  }

  public class Class1 { }
  public class Class2 { }

}

gives me this error -

No component for supporting the service complexresolutionioc.IChildInterface1`1[[complexresolutionioc.Class2, complexresolutionioc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] was found at Castle.MicroKernel.DefaultKernel.Resolve(Type service) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs:line 172 at Castle.Windsor.WindsorContainer.Resolve(Type service) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 889 at Castle.Windsor.WindsorContainer.ResolveT in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 978 at complexresolutionioc.P开发者_如何学运维rogram.Main(String[] args) in C:\MyDev\complexresolutionioc\complexresolutionioc\Program.cs:line 21

-ilias


You need to provide closed generic type you want to resolve:

var thisWorks = container.Resolve<IChildInterface1<Class2>>();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号