grumpydev/TinyIoC

Resolving generic type from parent container fails

Closed this issue · 3 comments

Resolving generic types registered to parent containers is not working.
The code snipped demonstrates this by registring a generic dictionary to a container, create a child container and try to resolve the dictionary.

            var parentContext = new TinyIoC.TinyIoCContainer();
            var dict = new Dictionary<string, string>();
            parentContext.Register<IDictionary<string, string>>(dict);

            var childContext = parentContext.GetChildContainer();
            var myDict = childContext.Resolve<IDictionary<string, string>>();  // TinyIoCResolutionException
            Assert.AreSame(dict, myDict);

I tried the same with named object registration, but still it's not working:

            var parentContext = new TinyIoC.TinyIoCContainer();
            var dict = new Dictionary<string, string>();
            parentContext.Register<IDictionary<string, string>>(dict, "MyDict");

            var childContext = parentContext.GetChildContainer();
            var myDict = childContext.Resolve<IDictionary<string, string>>("MyDict");  // TinyIoCResolutionException
            Assert.AreSame(dict, myDict);

As a workaround i registered the dictionary as "object" - then it's working. So it's for sure related to the generic type.

            var parentContext = new TinyIoC.TinyIoCContainer();
            var dict = new Dictionary<string, string>();
            parentContext.Register<object>(dict, "MyDict");

            var childContext = parentContext.GetChildContainer();
            var myDict = childContext.Resolve<object>("MyDict");
            Assert.AreSame(dict, myDict);   // OK

Would you mind submitting a pr and test for this.

Sorry, i was not yet able to find the issue. The code above are actually unit tests i used to check whether the problem is in my code or somewhere in TinyIoC. So they are usage examples...
Surely if i find the real issue i will create a PR!

Thanks for the PR!