Some of the provided bindings for the mutli injection may fail to resolve.
public List<IMyCommand> GetMyCommands()
{
//throws
return kernel.GetAll<IMyCommand>().ToList();
}
I want to still get all the successfully resolved objects, and ideally which ones failed. Is there a way to achieve this with Ninject?
Not out of the box. But we can create some kind of hack/Workaround. Caution. I would rather implement some specific mechanism which handles my case explicitly than to involve Ninject in that.
But for the curiuous minded, here you go:
If you have a look at the implementation of
IResolutionRoot.TryGet
you'll see that all it does is catchActivationException
and returndefault(T)
in that case.We can create our own
TryGetAll<T>
which does the same, but not for the entireIRequest
but rather for each binding separately. So here's how to do it:I've tested it and it works: