Skip to content

Commit 38f6830

Browse files
committed
fixed: #546 for v5.3.1
1 parent 97eda56 commit 38f6830

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

src/DryIoc/Container.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12191,7 +12191,10 @@ private static void MatchOpenGenericConstraints(Type[] implTypeParams, Type[] im
1219112191
var implTypeParamConstraint = implTypeParamConstraints[j];
1219212192
if (implTypeParamConstraint != implTypeArg && implTypeParamConstraint.IsOpenGeneric())
1219312193
{
12194-
var implTypeArgArgs = implTypeArg.IsGenericType ? implTypeArg.GetGenericArguments() : implTypeArg.One();
12194+
var implTypeArgArgs =
12195+
implTypeArg.IsGenericType ? implTypeArg.GetGenericArguments() :
12196+
implTypeArg.IsArray ? implTypeArg.GetElementType().One() :
12197+
implTypeArg.One();
1219512198
var implTypeParamConstraintParams = implTypeParamConstraint.GetGenericArguments();
1219612199

1219712200
constraintMatchFound = MatchServiceWithImplementedTypeParams(
@@ -12202,8 +12205,7 @@ private static void MatchOpenGenericConstraints(Type[] implTypeParams, Type[] im
1220212205
}
1220312206

1220412207
private static bool MatchServiceWithImplementedTypeParams(
12205-
Type[] resultImplArgs, Type[] implParams, Type[] serviceParams, Type[] serviceArgs,
12206-
int resultCount = 0)
12208+
Type[] resultImplArgs, Type[] implParams, Type[] serviceParams, Type[] serviceArgs)
1220712209
{
1220812210
if (serviceArgs.Length != serviceParams.Length)
1220912211
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
4+
namespace DryIoc.IssuesTests
5+
{
6+
[TestFixture]
7+
public class GHIssue546_Generic_type_constraint_resolution_doesnt_see_arrays_as_IEnumerable : ITest
8+
{
9+
public int Run()
10+
{
11+
Test1();
12+
Test2();
13+
return 2;
14+
}
15+
16+
[Test]
17+
public void Test1()
18+
{
19+
// Works - type is compliant with the generic constraints - would not compile otherwise.
20+
var q = new MyThing<int[], int>();
21+
22+
var container = new Container();
23+
24+
container.Register(typeof(IMyThing<>), typeof(MyThing<,>), Reuse.ScopedOrSingleton);
25+
26+
// Fails when we try to resolve it through the container
27+
var y = container.Resolve<IMyThing<int[]>>();
28+
Assert.IsInstanceOf<MyThing<int[], int>>(y);
29+
}
30+
31+
[Test]
32+
public void Test2()
33+
{
34+
var container = new Container();
35+
36+
container.Register(typeof(IMyThing<>), typeof(MyThing<,>), Reuse.ScopedOrSingleton);
37+
38+
// Works - constraint of IEnumerable<TValue> brings int from List<int> into the 2nd generic parameter.
39+
var x = container.Resolve<IMyThing<List<int>>>();
40+
Assert.IsInstanceOf<MyThing<List<int>, int>>(x);
41+
42+
// Works - type is compliant with the generic constraints - would not compile otherwise.
43+
var q = new MyThing<int[], int>();
44+
45+
// Fails when we try to resolve it through the container
46+
var y = container.Resolve<IMyThing<int[]>>();
47+
Assert.IsInstanceOf<MyThing<int[], int>>(y);
48+
}
49+
50+
public interface IMyThing<T> { }
51+
public interface IMyThing<T, TValue> : IMyThing<T>
52+
where T : IEnumerable<TValue> { }
53+
54+
public class MyThing<T, TValue> : IMyThing<T, TValue>
55+
where T : IEnumerable<TValue> { }
56+
}
57+
}

test/DryIoc.TestRunner/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ void Run(Func<int> run, string name = null)
6464
new GHIssue508_Throws_when_lazy_resolve_after_explicit_create_using_factory_func_from_within_scope(),
6565
new GHIssue471_Regression_v5_using_Rules_SelectKeyedOverDefaultFactory(),
6666
new GHIssue532_WithUseInterpretation_still_use_DynamicMethod_and_ILEmit(),
67-
new GHIssue536_DryIoc_Exception_in_a_Constructor_of_a_Dependency_does_tunnel_through_Resolve_call()
67+
new GHIssue536_DryIoc_Exception_in_a_Constructor_of_a_Dependency_does_tunnel_through_Resolve_call(),
68+
new GHIssue546_Generic_type_constraint_resolution_doesnt_see_arrays_as_IEnumerable()
6869
};
6970

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

0 commit comments

Comments
 (0)