diff --git a/src/DryIoc/Container.cs b/src/DryIoc/Container.cs index 8a2f17eea..7bf69dc38 100644 --- a/src/DryIoc/Container.cs +++ b/src/DryIoc/Container.cs @@ -8426,10 +8426,35 @@ public static class Resolver typeof(IResolver).Method(nameof(IResolver.Resolve), typeof(Type), typeof(object), typeof(IfUnresolved), typeof(Type), typeof(Request), typeof(object[])); - // todo: @wip add multiple overloads #498 - // internal static readonly MethodInfo ResolveMethod = - // typeof(IResolver).Method(nameof(IResolver.Resolve), typeof(Type), typeof(object), - // typeof(IfUnresolved), typeof(Type), typeof(Request), typeof(object[])); + /// Code Generation specific overload + public static object CodeGenResolve(this IResolver resolver, Type serviceType, Request parent) => + resolver.Resolve(serviceType, null, IfUnresolved.Throw, null, parent, null); + internal static readonly MethodInfo CodeGenResolveWithReqMethod = + typeof(Resolver).Method(nameof(Resolver.CodeGenResolve), typeof(IResolver), typeof(Type), typeof(Request)); + + /// Code Generation specific overload + public static object CodeGenResolve(this IResolver resolver, Type serviceType, Request parent, object[] args) => + resolver.Resolve(serviceType, null, IfUnresolved.Throw, null, parent, args); + internal static readonly MethodInfo CodeGenResolveWithReqAndArgsMethod = + typeof(Resolver).Method(nameof(Resolver.CodeGenResolve), typeof(IResolver), typeof(Type), typeof(Request), typeof(object[])); + + /// Code Generation specific overload + public static object CodeGenResolve(this IResolver resolver, Type serviceType, IfUnresolved ifUnresolved, Request parent) => + resolver.Resolve(serviceType, null, ifUnresolved, null, parent, null); + internal static readonly MethodInfo CodeGenResolveWithIfUnresolvedAndReqMethod = + typeof(Resolver).Method(nameof(Resolver.CodeGenResolve), typeof(IResolver), typeof(Type), typeof(IfUnresolved), typeof(Request)); + + /// Code Generation specific overload + public static object CodeGenResolve(this IResolver resolver, Type serviceType, Type requiredServiceType, Request parent) => + resolver.Resolve(serviceType, requiredServiceType, IfUnresolved.Throw, null, parent, null); + internal static readonly MethodInfo CodeGenResolveWithRequiredTypeAndReqMethod = + typeof(Resolver).Method(nameof(Resolver.CodeGenResolve), typeof(IResolver), typeof(Type), typeof(Type), typeof(Request)); + + /// Code Generation specific overload + public static object CodeGenResolve(this IResolver resolver, Type serviceType, object serviceKey, Request parent) => + resolver.Resolve(serviceType, serviceKey, IfUnresolved.Throw, null, parent, null); + internal static readonly MethodInfo CodeGenResolveWithServiceKeyAndReqMethod = + typeof(Resolver).Method(nameof(Resolver.CodeGenResolve), typeof(IResolver), typeof(Type), typeof(object), typeof(Request)); internal static readonly MethodInfo ResolveManyMethod = typeof(IResolver).GetMethod(nameof(IResolver.ResolveMany)); @@ -8443,8 +8468,7 @@ public static object Resolve(this IResolver resolver, Type serviceType, IfUnreso resolver.Resolve(serviceType, ifUnresolved); /// Resolves instance of type TService from container. - public static TService Resolve(this IResolver resolver, - IfUnresolved ifUnresolved = IfUnresolved.Throw) => + public static TService Resolve(this IResolver resolver, IfUnresolved ifUnresolved = IfUnresolved.Throw) => (TService)resolver.Resolve(typeof(TService), ifUnresolved); /// Tries to resolve instance of service type from container. diff --git a/test/DryIoc.IssuesTests/Issue_HandleVariance.cs b/test/DryIoc.IssuesTests/Issue_HandleVariance.cs index 77d29becd..54fa7d17b 100644 --- a/test/DryIoc.IssuesTests/Issue_HandleVariance.cs +++ b/test/DryIoc.IssuesTests/Issue_HandleVariance.cs @@ -4,27 +4,39 @@ namespace DryIoc.IssuesTests { [TestFixture] - public class Issue_HandleVariance + public class Issue_HandleVariance : ITest { - [Test, Explicit] // Ignore("fixme") todo: @bug? check why it is failing - public void CommandHandlers_CanBeResolved_From_IoC() + public int Run() + { + Test(); + return 1; + } + + [Test] + public void Test() { var container = new Container(); - container.Register(typeof(IBird<>), typeof(BirdBaseImpl<>)); - container.Register(typeof(IBird), typeof(BirdImpl)); + container.Register(typeof(IContain<>), typeof(ContainBirdBase<>)); + container.Register(typeof(IContain), typeof(ContainBird)); + + var birds = container.ResolveMany>().ToList(); + Assert.AreEqual(1, birds.Count()); + + var birdBases = container.ResolveMany>>(); + Assert.AreEqual(1, birdBases.Count()); - var services = container.ResolveMany>(); - Assert.AreEqual(2, services.Count()); + var birdsArray = container.Resolve[]>(); + Assert.AreEqual(1, birdsArray.Length); - var servicesArray = container.Resolve[]>(); - Assert.AreEqual(2, servicesArray.Length); + var birdBasesArray = container.Resolve>[]>(); + Assert.AreEqual(1, birdBasesArray.Length); } - public interface IBird { } + public interface IContain { } public class BirdBase { } public class Bird : BirdBase { } // IBird - public class BirdImpl : IBird { } - public class BirdBaseImpl : IBird> { } + public class ContainBird : IContain { } + public class ContainBirdBase : IContain> { } } } diff --git a/test/DryIoc.TestRunner.net472/Program.cs b/test/DryIoc.TestRunner.net472/Program.cs index c4b948a62..032a59e3f 100644 --- a/test/DryIoc.TestRunner.net472/Program.cs +++ b/test/DryIoc.TestRunner.net472/Program.cs @@ -69,6 +69,7 @@ public static void RunAllTests() var issueTests = new ITest[] { new Issue_Can_resolve_singleton_with_Func_of_scoped_dependency(), + new Issue_HandleVariance(), new Issue_InjectingSerilogLogger(), new ParameterResolutionFixture(), new Issue_Register_null_string(), diff --git a/test/DryIoc.TestRunner/Program.cs b/test/DryIoc.TestRunner/Program.cs index 3505499e6..528ade4f8 100644 --- a/test/DryIoc.TestRunner/Program.cs +++ b/test/DryIoc.TestRunner/Program.cs @@ -69,6 +69,7 @@ public static void RunAllTests() var issueTests = new ITest[] { new Issue_Can_resolve_singleton_with_Func_of_scoped_dependency(), + new Issue_HandleVariance(), new Issue_InjectingSerilogLogger(), new ParameterResolutionFixture(), new Issue_Register_null_string(),