grumpydev/TinyIoC

Injecting IEnumerable<Type> to constructor - unnamed registrations

Opened this issue · 1 comments

Hi guys,

I found out the following issue:

When I register multiple implementations for the same interface - but do it individually one by one, the unnamed one (if present) isn't resolved when using constructor injection of IEnumerable. On the other hand, it works when using ResolveAll method.

Consider following registration example:

TinyIoCContainer.Current.Register<Interface, Impl1>(); TinyIoCContainer.Current.Register<Interface, Impl2>("2");

When calling

TinyIoCContainer.Current.ResolveAll<Interface>();

resulting list contains both of registered implementations. But if it's used this way:

TinyIoCContainer.Current.Resolve<CtorTest>();

where class CtorTest looks like this:

`
public class CtorTest
{

	public CtorTest(IEnumerable<Interface> impls)
	{
	}
}

`

resulting list contains only named registrations. When I was digging in sources, I found out that in following method:

`
private object GetIEnumerableRequest(Type type)
{

        #if NETFX_CORE
        		var genericResolveAllMethod = this.GetType().GetGenericMethod("ResolveAll", type.GenericTypeArguments, new[] { typeof(bool) });
        #else
        var genericResolveAllMethod = this.GetType().GetGenericMethod(BindingFlags.Public | BindingFlags.Instance, "ResolveAll", type.GetGenericArguments(), new[] { typeof(bool) });
        //#endif
        return genericResolveAllMethod.Invoke(this, new object[] { false });
    }

`

the last row invokes method with last parameter set to false . This parameter is actually includeUnnamed parameter in ResolveAll method. When ResolveAll is called directly (as in fisrst example) it falls to default call, where includeUnnamed is set to true. That's why it works in first example, but doesn't work in second.

My question is - is that a purpose ? If so, can you examplain me why?

Thanks,
Patrik

I'm really not sure on that one, but to me it should be resolved as that's the behavior I've seen with the new Microsoft di. Can you create some tests covering all scenarios and add a pr for this.