Skip to content

Commit

Permalink
Merge pull request autofac#526 from alexandrnikitin/Issue507_AsClosed…
Browse files Browse the repository at this point in the history
…TypesOf_bug

Fix for autofac#507 AsClosedTypesOf does not register component as non-generic interface
  • Loading branch information
alexmg committed May 21, 2014
2 parents 2525657 + a28c666 commit 29cb043
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Core/Source/Autofac/Util/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static IEnumerable<Type> GetTypesThatClose(this Type @this, Type openGene
static IEnumerable<Type> FindAssignableTypesThatClose(Type candidateType, Type openGenericServiceType)
{
return TypesAssignableFrom(candidateType)
.Where(t => IsGenericTypeDefinedBy(t, openGenericServiceType));
.Where(t => t.IsClosedTypeOf(openGenericServiceType));
}

static IEnumerable<Type> TypesAssignableFrom(Type candidateType)
Expand All @@ -75,6 +75,14 @@ public static bool IsGenericTypeDefinedBy(this Type @this, Type openGeneric)
return !@this.ContainsGenericParameters && @this.IsGenericType && @this.GetGenericTypeDefinition() == openGeneric;
}

public static bool IsClosedTypeOf(this Type @this, Type openGeneric)
{
if (@this == null) throw new ArgumentNullException("this");
if (openGeneric == null) throw new ArgumentNullException("openGeneric");

return TypesAssignableFrom(@this).Any(t => t.IsGenericType && !@this.ContainsGenericParameters && t.GetGenericTypeDefinition() == openGeneric);
}

public static bool IsDelegate(this Type type)
{
if (type == null) throw new ArgumentNullException("type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@
<Compile Include="BComponent.cs" />
<Compile Include="A2Component.cs" />
<Compile Include="BModule.cs" />
<Compile Include="CloseCommand.cs" />
<Compile Include="CommandBase.cs" />
<Compile Include="DeleteCommand.cs" />
<Compile Include="DeleteCommandData.cs" />
<Compile Include="HasNestedFactoryDelegate.cs" />
<Compile Include="IAService.cs" />
<Compile Include="IBService.cs" />
<Compile Include="ICloseCommand.cs" />
<Compile Include="ICommand.cs" />
<Compile Include="Message.cs" />
<Compile Include="MetadataAttributeScanningScenario.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Autofac.Tests.Scenarios.ScannedAssembly
{
public class CloseCommand : ICloseCommand
{
public void Execute(object data)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Autofac.Tests.Scenarios.ScannedAssembly
{
public interface ICloseCommand : ICommand<object>
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ public void AsClosedTypesOfOpenGenericAbstractClassTypeProvidedClosingGenericTyp
Assert.That(c.Resolve<Message<string>>(), Is.TypeOf<StringMessage>());
}

[Test]
public void AsClosedTypesOfClosingInterfaceTypeRegistered()
{
var cb = new ContainerBuilder();
cb.RegisterAssemblyTypes(typeof(ICloseCommand).Assembly)
.AsClosedTypesOf(typeof(ICommand<>));
var c = cb.Build();

Assert.That(c.Resolve<ICloseCommand>(), Is.TypeOf<CloseCommand>());
}

[Test]
public void AsSelfExposesConcreteTypeAsService()
{
Expand Down

0 comments on commit 29cb043

Please sign in to comment.