Skip to content

Commit 13ea57d

Browse files
authored
PR #1045: Fix #1040 - IsRegistered throws IndexOutOfRangeException for open generic type
2 parents cf28582 + fb145e4 commit 13ea57d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Autofac/Features/OpenGenerics/OpenGenericServiceBinder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static bool TryBindServiceType(
4444
out Service[] constructedServices)
4545
{
4646
var swt = service as IServiceWithType;
47-
if (swt != null && swt.ServiceType.GetTypeInfo().IsGenericType)
47+
if (swt != null && swt.ServiceType.GetTypeInfo().IsGenericType && !swt.ServiceType.IsGenericTypeDefinition)
4848
{
4949
var definitionService = (IServiceWithType)swt.ChangeType(swt.ServiceType.GetGenericTypeDefinition());
5050
var serviceGenericArguments = swt.ServiceType.GetTypeInfo().GenericTypeArguments;

test/Autofac.Test/Features/OpenGenerics/ComplexGenericsTests.cs

+11
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ public void ResolveTypeFromOpenGenericInterfaceTypeParameterIsInterfaceWithConst
341341
var instance = container.Resolve<INested<ISimpleInterface>>();
342342
}
343343

344+
[Fact]
345+
public void CheckGenericTypeIsRegisteredWhenNotSpecifyParameterType()
346+
{
347+
// Issue #1040: IsRegistered throws IndexOutOfRangeException for open generic type
348+
var cb = new ContainerBuilder();
349+
cb.RegisterGeneric(typeof(C<>));
350+
var container = cb.Build();
351+
352+
Assert.False(container.IsRegistered(typeof(C<>)));
353+
}
354+
344355
private class CNestedSimpleInterface : INested<ISimpleInterface>
345356
{
346357
}

0 commit comments

Comments
 (0)