From a883fdc402bd448bcc07c22c1cb8b4378d861f41 Mon Sep 17 00:00:00 2001 From: dadhi Date: Thu, 30 Dec 2021 22:29:26 +0100 Subject: [PATCH] fixed the Issue_Register_null_string --- src/DryIoc/Container.cs | 3 +- .../Issue_Register_null_string.cs | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/DryIoc.IssuesTests/Issue_Register_null_string.cs diff --git a/src/DryIoc/Container.cs b/src/DryIoc/Container.cs index ea9c71f3..b0f90cfa 100644 --- a/src/DryIoc/Container.cs +++ b/src/DryIoc/Container.cs @@ -11819,7 +11819,8 @@ public override Expression CreateExpressionOrDefault(Request request) // otherwise just return a constant var instanceExpr = request.Container.GetConstantExpression(Instance); var serviceType = request.GetActualServiceType(); - if (serviceType.GetTypeInfo().IsAssignableFrom(ImplementationType.GetTypeInfo())) + var implType = ImplementationType; + if (implType != null && serviceType.GetTypeInfo().IsAssignableFrom(implType.GetTypeInfo())) return instanceExpr; return Convert(instanceExpr, serviceType); } diff --git a/test/DryIoc.IssuesTests/Issue_Register_null_string.cs b/test/DryIoc.IssuesTests/Issue_Register_null_string.cs new file mode 100644 index 00000000..24fc4381 --- /dev/null +++ b/test/DryIoc.IssuesTests/Issue_Register_null_string.cs @@ -0,0 +1,57 @@ +using System; +using NUnit.Framework; + +namespace DryIoc.IssuesTests +{ + [TestFixture] + public class Issue_Register_null_string + { + [Test] + public void Test() + { + var rules = Rules.Default.WithConcreteTypeDynamicRegistrations(reuse: Reuse.Transient) + .With(Made.Of(FactoryMethod.ConstructorWithResolvableArguments)) + .WithDefaultIfAlreadyRegistered(IfAlreadyRegistered.Replace) + .WithFuncAndLazyWithoutRegistration() + .WithTrackingDisposableTransients() + .WithoutFastExpressionCompiler() + .WithFactorySelector(Rules.SelectLastRegisteredFactory()); + + var c = new Container(rules); + + c.Register(); + + // Both variants work with DryIoc v5 (preview) but failing with the v4.8.4 + //c.RegisterDelegate(() => null, setup: Setup.With(asResolutionCall: true)); + c.RegisterInstance(null, setup: Setup.With(asResolutionCall: true)); + + var s = c.Resolve(); + var a = c.Resolve(); + + var a2 = c.Resolve(new object[] { "Hello World" }); + + c.RegisterInstance(null); + var a3 = c.Resolve(); + + c.RegisterInstance("Hello New Registration"); + var a4 = c.Resolve(); + + c.RegisterDelegate(() => "Hello Another Registration"); + var a5 = c.Resolve(); + + Console.WriteLine("Hello s: " + (s ?? "null")); + Console.WriteLine("Hello a.S: " + (a.S ?? "null")); + Console.WriteLine("Hello a2.S: " + (a2.S ?? "null")); + Console.WriteLine("Hello a3.S: " + (a3.S ?? "null")); + + Console.WriteLine("Hello a4.S: " + (a4.S ?? "null")); + Console.WriteLine("Hello a5.S: " + (a5.S ?? "null")); + } + + class A + { + public string S; + public A(string s) => S = s; + } + } +}