Skip to content

Commit

Permalink
fixed the Issue_Register_null_string
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Dec 30, 2021
1 parent f715212 commit a883fdc
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
57 changes: 57 additions & 0 deletions test/DryIoc.IssuesTests/Issue_Register_null_string.cs
Original file line number Diff line number Diff line change
@@ -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<A>();

// Both variants work with DryIoc v5 (preview) but failing with the v4.8.4
//c.RegisterDelegate<string>(() => null, setup: Setup.With(asResolutionCall: true));
c.RegisterInstance<string>(null, setup: Setup.With(asResolutionCall: true));

var s = c.Resolve<string>();
var a = c.Resolve<A>();

var a2 = c.Resolve<A>(new object[] { "Hello World" });

c.RegisterInstance<string>(null);
var a3 = c.Resolve<A>();

c.RegisterInstance<string>("Hello New Registration");
var a4 = c.Resolve<A>();

c.RegisterDelegate<string>(() => "Hello Another Registration");
var a5 = c.Resolve<A>();

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;
}
}
}

0 comments on commit a883fdc

Please sign in to comment.