Skip to content

Commit

Permalink
CollectionResolver supports all default collection types now
Browse files Browse the repository at this point in the history
That includes IReadOnlyCollection<> and IReadOnlyList<> in .NET 4.5 (or whatever Array types decide to implement in the future)
  • Loading branch information
kkozmic committed Sep 16, 2013
1 parent 6a5e6f4 commit fd20561
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Castle.Windsor.Tests/Castle.Windsor.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
<Compile Include="Components\ADisposable.cs" />
<Compile Include="Components\AppHost.cs" />
<Compile Include="Components\AppScreenCBA.cs" />
<Compile Include="Components\ArrayDepAsConstructor.cs" />
<Compile Include="Components\BDisposable.cs" />
<Compile Include="Components\BoundComponent.cs" />
<Compile Include="Components\CacheWithClassConstraint.cs" />
Expand Down Expand Up @@ -491,6 +492,7 @@
<Compile Include="Components\NeedsGenericType.cs" />
<Compile Include="Components\NullCache.cs" />
<Compile Include="Components\PlasmaGunArm.cs" />
<Compile Include="Components\ReadOnlyListDepAsConstructor.cs" />
<Compile Include="Components\ResolvableDependency.cs" />
<Compile Include="Components\ReviewerRepository.cs" />
<Compile Include="Components\ServiceImplGeneric.cs" />
Expand Down Expand Up @@ -616,7 +618,7 @@
<Compile Include="ComponentsWithAttribute\HasUserAttributeNonRegister.cs" />
<Compile Include="ComponentsWithAttribute\HasUserAttributeRegister.cs" />
<Compile Include="ComponentsWithAttribute\UserAttribute.cs" />
<Compile Include="Components\ArrayDepAsConstructor.cs" />
<Compile Include="Components\ReadOnlyCollectionDepAsConstructor.cs" />
<Compile Include="Components\ArrayDepAsProperty.cs" />
<Compile Include="Components\CollectionDepAsConstructor.cs" />
<Compile Include="Components\CollectionDepAsProperty.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2004-2011 Castle Project - http://www.castleproject.org/
// Copyright 2004-2013 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2004-2013 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace CastleTests.Components
{
#if DOTNET45
using System.Collections.Generic;

public class ReadOnlyCollectionDepAsConstructor
{
private readonly IReadOnlyCollection<IEmptyService> services;

public ReadOnlyCollectionDepAsConstructor(IReadOnlyCollection<IEmptyService> services)
{
this.services = services;
}

public IReadOnlyCollection<IEmptyService> Services
{
get { return services; }
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2004-2013 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace CastleTests.Components
{
using System.Collections.Generic;

#if DOTNET45
public class ReadOnlyListDepAsConstructor
{
private readonly IReadOnlyList<IEmptyService> services;

public ReadOnlyListDepAsConstructor(IReadOnlyList<IEmptyService> services)
{
this.services = services;
}

public IReadOnlyList<IEmptyService> Services
{
get { return services; }
}
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Castle.MicroKernel.Tests.SpecializedResolvers
using System;
using System.Linq;

using Castle.Core.Internal;
using Castle.MicroKernel.Handlers;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.Resolvers.SpecializedResolvers;
Expand Down Expand Up @@ -80,6 +81,43 @@ public void DependencyOnArrayOfServices_OnConstructor()
}
}

#if DOTNET45
[Test]
public void DependencyOn_Readonly_collection_OfServices_OnConstructor()
{
Container.Register(Component.For<IEmptyService>().ImplementedBy<EmptyServiceA>(),
Component.For<IEmptyService>().ImplementedBy<EmptyServiceB>(),
Component.For<ReadOnlyCollectionDepAsConstructor>());

var component = Container.Resolve<ReadOnlyCollectionDepAsConstructor>();

Assert.IsNotNull(component.Services);
Assert.AreEqual(2, component.Services.Count);
foreach (var service in component.Services)
{
Assert.IsNotNull(service);
}
}


[Test]
public void DependencyOn_Readonly_list_OfServices_OnConstructor()
{
Container.Register(Component.For<IEmptyService>().ImplementedBy<EmptyServiceA>(),
Component.For<IEmptyService>().ImplementedBy<EmptyServiceB>(),
Component.For<ReadOnlyListDepAsConstructor>());

var component = Container.Resolve<ReadOnlyListDepAsConstructor>();

Assert.IsNotNull(component.Services);
Assert.AreEqual(2, component.Services.Count);
foreach (var service in component.Services)
{
Assert.IsNotNull(service);
}
}
#endif

[Test]
public void DependencyOnArrayOfServices_OnConstructor_empty_allowed_empty_provided()
{
Expand Down
9 changes: 6 additions & 3 deletions src/Castle.Windsor/Core/Internal/ReflectionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ namespace Castle.Core.Internal

public static class ReflectionUtil
{
public static readonly Type[] OpenGenericArrayInterfaces = typeof(object[]).GetInterfaces()
.Where(i => i.IsGenericType)
.Select(i => i.GetGenericTypeDefinition())
.ToArray();

#if !(DOTNET35 || SILVERLIGHT)
private static readonly ConcurrentDictionary<ConstructorInfo, Func<object[], object>> factories =
new ConcurrentDictionary<ConstructorInfo, Func<object[], object>>();
Expand Down Expand Up @@ -210,9 +215,7 @@ public static Type GetCompatibleArrayItemType(this Type type)
return null;
}
var openGeneric = type.GetGenericTypeDefinition();
if (openGeneric == typeof(IList<>) ||
openGeneric == typeof(ICollection<>) ||
openGeneric == typeof(IEnumerable<>))
if (OpenGenericArrayInterfaces.Contains(openGeneric))
{
return type.GetGenericArguments()[0];
}
Expand Down

0 comments on commit fd20561

Please sign in to comment.