Skip to content

Commit

Permalink
Allow registering fully closed generic types, even if arity doesn't m…
Browse files Browse the repository at this point in the history
…atch #104
  • Loading branch information
khellang committed Sep 25, 2024
1 parent 5221bff commit 481f71b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Scrutor/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ public static bool HasMatchingGenericArity(this Type interfaceType, Type type)
{
if (interfaceType.IsGenericType)
{
if (!interfaceType.ContainsGenericParameters)
{
return true;
}

var argumentCount = interfaceType.GetGenericArguments().Length;
var parameterCount = type.GetGenericArguments().Length;

Expand Down
13 changes: 13 additions & 0 deletions test/Scrutor.Tests/ScanningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ public void CanRegisterGenericTypes()
Assert.Equal(typeof(QueryHandler), service.ImplementationType);
}

[Fact]
public void CanRegisterFullyClosedGenericWithDifferentArityThanServiceType()
{
Collection.Scan(scan => scan
.FromTypes(typeof(PartiallyClosedGeneric<string>))
.AsImplementedInterfaces()
.WithTransientLifetime());

var descriptor = Assert.Single(Collection);

Assert.Equal(typeof(IPartiallyClosedGeneric<string, int>), descriptor.ServiceType);
}

[Fact]
public void CanScanUsingAttributes()
{
Expand Down

0 comments on commit 481f71b

Please sign in to comment.