Resolve generic interface with Windsor without knowing the type at compile time?
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.
精彩评论