gpt4 book ai didi

generics - 如何让CasSTLe Windsor解析带有约束的泛型?

转载 作者:行者123 更新时间:2023-12-03 02:49:22 26 4
gpt4 key购买 nike

using System;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
using System.Reflection;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;

namespace Windsor
{
class MainClass
{
public static void Main (string[] args)
{
var container = new WindsorContainer ();

container.Register (Component.For (typeof(IIface<, >)).ImplementedBy (typeof(HandlerImpl<, >)));
//container.Register (Component.For (typeof(IIface<, >)).ImplementedBy(typeof(Impl2)));
container.Kernel.Resolver.AddSubResolver (new ArrayResolver (container.Kernel));
var normal = container.ResolveAll<IIface<Impl2, Stub>> ();
var ex = container.ResolveAll<IIface<Impl1, Stub>> ();

//var qwe = new HandlerImpl<Impl1, Stub> ();

Console.WriteLine("Hello World!");
}
}

public class Base {}

public class Stub {}

public interface AdditionalIface
{
}

public interface IIface<T1, T2> where T1 : Base where T2 : class
{
T1 Command { get; set; }
}

public class HandlerImpl<T1, T2> : IIface<T1, T2> where T1 : Base, AdditionalIface where T2 : class
{
public T1 Command { get; set; }
}

public class Impl1 : Base
{
}

public class Impl2 : Base, AdditionalIface
{
}
}

所以,现在如果我这样做:

var normal = container.ResolveAll<IIface<Impl2, Stub>> (); // this works ok
var ex = container.ResolveAll<IIface<Impl1, Stub>> (); // this throws exception abou not fullfilled constraints
// instead i want it just show no resolved implementations

有什么办法可以让这个工作如我所愿吗?

最佳答案

实际上这似乎是 Windsor 代码中的一个错误。

更新:

现在已经修复了

关于generics - 如何让CasSTLe Windsor解析带有约束的泛型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3071000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com