From 481f71bb3efd7735aa52eb7b6d7b96427bfdec82 Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Wed, 25 Sep 2024 12:28:40 +0200 Subject: [PATCH] Allow registering fully closed generic types, even if arity doesn't match #104 --- src/Scrutor/ReflectionExtensions.cs | 5 +++++ test/Scrutor.Tests/ScanningTests.cs | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Scrutor/ReflectionExtensions.cs b/src/Scrutor/ReflectionExtensions.cs index a477df4..e8e41ed 100644 --- a/src/Scrutor/ReflectionExtensions.cs +++ b/src/Scrutor/ReflectionExtensions.cs @@ -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; diff --git a/test/Scrutor.Tests/ScanningTests.cs b/test/Scrutor.Tests/ScanningTests.cs index 46f2d7d..57b623f 100644 --- a/test/Scrutor.Tests/ScanningTests.cs +++ b/test/Scrutor.Tests/ScanningTests.cs @@ -220,6 +220,19 @@ public void CanRegisterGenericTypes() Assert.Equal(typeof(QueryHandler), service.ImplementationType); } + [Fact] + public void CanRegisterFullyClosedGenericWithDifferentArityThanServiceType() + { + Collection.Scan(scan => scan + .FromTypes(typeof(PartiallyClosedGeneric)) + .AsImplementedInterfaces() + .WithTransientLifetime()); + + var descriptor = Assert.Single(Collection); + + Assert.Equal(typeof(IPartiallyClosedGeneric), descriptor.ServiceType); + } + [Fact] public void CanScanUsingAttributes() {