开发者

Resolve generic interface with Windsor without knowing the type at compile time?

开发者 https://www.devze.com 2023-02-28 10:23 出处:网络
With Windsor I can do this: var validators = container.ResolveAll<IEntityValidator<Product>>();

With Windsor I can do this:

var validators = container.ResolveAll<IEntityValidator<Product>>();

But I don't know the type at compile. I need code more like this:

var type = obj.GetType();
var validators = container.ResolveAll<IEntityValidator<...typ开发者_Python百科e...>>();

Obviously, the code above is not near the correct solution. I'm guessing there's some relfection magic needed. If it's at all possible with Windsor. Is it?


This might not be quite complete but it's possibly close to what you want to do:

var typeParam = obj.GetType();
var type = typeof(IEntityValidator<>).MakeGenericType(typeParam);
container.Resolve(type);


You can hand-craft the type you want using reflection. Something like this:

var wantedGenericParam = obj.GetType();
var genericType = typeof(IEntityValidator<>).MakeGenericType(wantedGenericParam);
var allValidators = container.ResolveAll(genericType);


The short answer is no. What gets done by container.ResolveAll<IEntityValidator<Product>>() is decided at compile time not at run time. It is the execution that takes place at run time.

0

精彩评论

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