Skip to content

Commit

Permalink
resolve generics constrained with generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
zacateras authored and alexmg committed Jan 3, 2017
1 parent fff276e commit 36e266f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Autofac/Util/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public static bool IsCompatibleWithGenericParameterConstraints(this Type generic
var parameterTypeInfo = parameter.GetTypeInfo();

if (argumentDefinitionTypeInfo.GetGenericParameterConstraints()
.Select(constraint => SubstituteGenericParameterConstraint(parameters, constraint))
.Any(constraint => !ParameterCompatibleWithTypeConstraint(parameter, constraint)))
{
return false;
Expand Down Expand Up @@ -160,6 +161,12 @@ private static IEnumerable<Type> FindAssignableTypesThatClose(Type candidateType
.Where(t => t.IsClosedTypeOf(openGenericServiceType));
}

private static Type SubstituteGenericParameterConstraint(Type[] parameters, Type constraint)
{
if (!constraint.IsGenericParameter) return constraint;
return parameters[constraint.GenericParameterPosition];
}

private static bool ParameterCompatibleWithTypeConstraint(Type parameter, Type constraint)
{
return constraint.GetTypeInfo().IsAssignableFrom(parameter.GetTypeInfo()) ||
Expand Down
19 changes: 18 additions & 1 deletion test/Autofac.Test/Features/OpenGenerics/ComplexGenericsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,27 @@ public void CanResolveComponentWhenConstraintsAreNested()
builder.RegisterGeneric(typeof(MultiConstrained<,>));

var container = builder.Build();

Assert.True(container.IsRegistered<MultiConstrained<int, IConstrainedConstraintWithOnlyAddedArgument<string>>>());
}

public class ConstrainedWithGenericParameter<T1, T2>
where T1 : T2
{
}

[Fact]
public void CanResolveComponentWhenGenericParameterIsConstrainedWithOtherGenericParameter()
{
var builder = new ContainerBuilder();

builder.RegisterGeneric(typeof(ConstrainedWithGenericParameter<,>));

var container = builder.Build();

Assert.True(container.IsRegistered<ConstrainedWithGenericParameter<int, object>>());
}

[Fact(Skip = "Issue #688")]
public void GenericArgumentArityDifference()
{
Expand Down

0 comments on commit 36e266f

Please sign in to comment.