有了 Windsor,我可以做到这一点:
var validators = container.ResolveAll<IEntityValidator<Product>>();
但我不知道编译时的类型。我需要更像这样的代码:
var type = obj.GetType();
var validators = container.ResolveAll<IEntityValidator<...type...>>();
显然,上面的代码不是正确的解决方案。我猜需要一些反射魔法。如果温莎有可能的话。是吗?
最佳答案
这可能不是很完整,但它可能接近您想要做的事情:
var typeParam = obj.GetType();
var type = typeof(IEntityValidator<>).MakeGenericType(typeParam);
container.Resolve(type);
关于c# - 在编译时不知道类型的情况下使用 Windsor 解析泛型接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5745015/