Skip to content

Commit

Permalink
fixed: #472
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Apr 22, 2022
1 parent 3c8ba34 commit 8f6ba56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
38 changes: 22 additions & 16 deletions src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3287,40 +3287,46 @@ private static bool TryInterpretNestedLambda(IResolverContext r, LambdaExpressio
}
else if (paramCount == 2)
{
var paramExpr0 = lambdaExpr.GetParameter(0);
var paramExpr1 = lambdaExpr.GetParameter(1);
var p0 = lambdaExpr.GetParameter(0);
var p1 = lambdaExpr.GetParameter(1);
if (returnType != typeof(void))
{
result = new Func<object, object, object>((arg0, arg1) =>
TryInterpretNestedLambdaBodyAndUnwrapException(r, bodyExpr, lambdaExpr, new[] { arg0, arg1 }, parentArgs));
result = new Func<object, object, object>((a0, a1) =>
TryInterpretNestedLambdaBodyAndUnwrapException(r, bodyExpr, lambdaExpr, new[] { a0, a1 }, parentArgs));

if (paramExpr0.Type != typeof(object) || paramExpr1.Type != typeof(object) || returnType != typeof(object))
result = _convertTwoArgFuncMethod.MakeGenericMethod(paramExpr0.Type, paramExpr1.Type, returnType).Invoke(null, new[] { result });
if (p0.Type != typeof(object) || p1.Type != typeof(object) || returnType != typeof(object))
result = _convertTwoArgFuncMethod.MakeGenericMethod(p0.Type, p1.Type, returnType).Invoke(null, new[] { result });
}
else
{
result = new Action<object, object>((arg0, arg1) =>
TryInterpretNestedLambdaBodyAndUnwrapException(r, bodyExpr, lambdaExpr, new[] { arg0, arg1 }, parentArgs));
result = new Action<object, object>((a0, a1) =>
TryInterpretNestedLambdaBodyAndUnwrapException(r, bodyExpr, lambdaExpr, new[] { a0, a1 }, parentArgs));

if (paramExpr0.Type != typeof(object) || paramExpr1.Type != typeof(object))
result = _convertTwoArgActionMethod.MakeGenericMethod(paramExpr0.Type, paramExpr1.Type).Invoke(null, new[] { result });
if (p0.Type != typeof(object) || p1.Type != typeof(object))
result = _convertTwoArgActionMethod.MakeGenericMethod(p0.Type, p1.Type).Invoke(null, new[] { result });
}
}
else
else
{
// todo: @feature support interpretation of the lambdas with 5+ parameters
if (paramCount > 4)
return false;

if (returnType != typeof(void))
{
result = new Func<object[], object>(args =>
TryInterpretNestedLambdaBodyAndUnwrapException(r, bodyExpr, lambdaExpr, args, parentArgs));
result = _convertThreeArgFuncMethod.MakeGenericMethod(GetParamsAndReturnType(lambdaExpr, returnType))
.Invoke(null, new[] { result });

var convertMethod = paramCount == 3 ? _convertThreeArgFuncMethod : _convertFourArgFuncMethod;
result = convertMethod.MakeGenericMethod(GetParamsAndReturnType(lambdaExpr, returnType)).Invoke(null, new[] { result });
}
else
{
result = new Action<object[]>(args =>
TryInterpretNestedLambdaBodyAndUnwrapException(r, bodyExpr, lambdaExpr, args, parentArgs));
result = _convertThreeArgActionMethod.MakeGenericMethod(GetParamTypes(lambdaExpr))
.Invoke(null, new[] { result });

var convertMethod = paramCount == 3 ? _convertThreeArgActionMethod : _convertFourArgActionMethod;
result = convertMethod.MakeGenericMethod(GetParamTypes(lambdaExpr)).Invoke(null, new[] { result });
}
}

Expand All @@ -3344,7 +3350,7 @@ private static Type[] GetParamsAndReturnType(IParameterProvider ps, Type returnT
{
var count = ps.ParameterCount;
var ts = new Type[count + 1];
for (var i = 0; i < ts.Length; ++i)
for (var i = 0; i < count; ++i)
ts[i] = ps.GetParameter(i).Type;
ts[count] = returnType;
return ts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int Run()
[Test]
public void Test()
{
var c = new Container();
var c = new Container(Rules.Default.WithUseInterpretation());

c.Register<IServiceB, ServiceB>();
c.Register<IDep, Dep>();
Expand All @@ -32,9 +32,9 @@ public void Test()
class ServiceA
{
public IEnumerable<IServiceB> Services;
public ServiceA(Func<ServiceA, IEnumerable<IServiceB>> onStart)
public ServiceA(Func<ServiceA, string, int, bool, IEnumerable<IServiceB>> onStart)
{
Services = onStart(this);
Services = onStart(this, "!", 42, true);
}
}

Expand Down
8 changes: 3 additions & 5 deletions test/DryIoc.TestRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ public static void Main()
{
RunAllTests();

// new GHIssue460_Getting_instance_from_parent_scope_even_if_replaced_by_Use().Run();
// new OpenGenericsTests().Run();
// new GHIssue391_Deadlock_during_Resolve().Run();
// new GHIssue470_Regression_v5_when_resolving_Func_of_IEnumerable_of_IService_with_Parameter().Run();

//new DynamicRegistrationsTests().Run();
// new GHIssue399_Func_dependency_on_Singleton_resolved_under_scope_breaks_after_disposing_scope_when_WithFuncAndLazyWithoutRegistration().Run();
// ObjectLayoutInspector.TypeLayout.PrintLayout<Request>();
}

Expand Down Expand Up @@ -60,6 +56,8 @@ void Run(Func<int> run, string name = null)
new GHIssue406_Allow_the_registration_of_the_partially_closed_implementation_type(),
new GHIssue460_Getting_instance_from_parent_scope_even_if_replaced_by_Use(),
new Issue548_After_registering_a_factory_Func_is_returned_instead_of_the_result_of_Func(),
new GHIssue470_Regression_v5_when_resolving_Func_of_IEnumerable_of_IService_with_Parameter(),
new GHIssue471_Regression_v5_using_Rules_SelectKeyedOverDefaultFactory(),
};

// Parallel.ForEach(tests, x => Run(x.Run)); // todo: @perf enable and test when more tests are added
Expand Down

0 comments on commit 8f6ba56

Please sign in to comment.