Castle Windsor resolving a generic interface

主宰稳场 提交于 2020-02-01 07:24:29

问题


I have generic interface with some implementations.

interface IBaseInterface<in TObject, out TDetails>
{
    TDetails GetDetails(TObject obj);
}

abstract class BaseImpl<TObject> : IBaseInterface<TObject, string>
{
    public abstract string GetDetails(TObject obj);
}

class Impl0 : BaseImpl<decimal>
{
    public override string GetDetails(decimal obj)
    {
        return "decimal";
    }
}

class Impl1 : BaseImpl<string>
{
    public override string GetDetails(string obj)
    {
        return "string";
    }
}

class Impl2 : BaseImpl<Details>
{
    public override string GetDetails(Details obj)
    {
        return "Details";
    }
}

class Impl3<TDetail> : BaseImpl<DetailsGeneric<TDetail>> where TDetail : Details
{
    public override string GetDetails(DetailsGeneric<TDetail> obj)
    {
        return "GenericDetails";
    }
}

class Details
{

}

class Details1 : Details
{

}

class DetailsGeneric<TDetails> : Details
{

}

Using WinsdorInstaller class for register components

container.Register(AllTypes.From(typeof(Program).Assembly.GetTypes())
  .BasedOn(typeof(IBaseInterface<,>)).WithServiceAllInterfaces()
  .LifestyleTransient());

Try resolve. Last two calls throwing exceptions, but I expect that they return components declared for base types

class Program
{
    static void Main(string[] args)
    {
        var container = new WindsorContainer();

        container.Install(new WindsorInstaller());

        Details("test", container);

        Details(123m, container);

        Details(new Details(), container);

        Details(new DetailsGeneric<Details>(), container);

        Details(new Details1(), container); // "ComponentNotFound" exception

        Details(new DetailsGeneric<Details1>(), container); // "ComponentNotFound" exception           
    }

    static void Details<TType>(TType obj, WindsorContainer container)
    {
        var details = container.Resolve<IBaseInterface<TType, string>>().GetDetails(obj);

        Console.WriteLine(details);
    }
}

Where i am wrong? and can I fix it?


回答1:


You don't have any component closed over Details1 from what I can see...



来源:https://stackoverflow.com/questions/13120390/castle-windsor-resolving-a-generic-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!