Skip to content

Commit

Permalink
Add EnumerableExtensions to implement ForEach and ConvertAll.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alina Popa committed May 17, 2017
1 parent 05085ce commit c52f999
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
42 changes: 42 additions & 0 deletions src/Castle.Windsor/Core/Internal/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2004-2011 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 Castle.Core.Internal
{
using System;
using System.Collections.Generic;

static class EnumerableExtensions
{
public static Y[] ConvertAll<X, Y>(this X[] items, Func<X, Y> converter)
{
var results = new List<Y>();

foreach (var item in items)
{
var y = converter(item);

results.Add(y);
}

return results.ToArray();
}

public static void ForEach<X>(this IEnumerable<X> items, Action<X> action)
{
foreach (var item in items)
action(item);
}
}
}
2 changes: 1 addition & 1 deletion src/Castle.Windsor/Core/Internal/ReflectionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private static AssemblyName GetAssemblyName(string filePath)
private static TBase Instantiate<TBase>(Type subtypeofTBase, object[] ctorArgs)
{
ctorArgs = ctorArgs ?? new object[0];
var types = ctorArgs.Select(a => a == null ? typeof(object) : a.GetType()).ToArray();
var types = ctorArgs.ConvertAll(a => a == null ? typeof(object) : a.GetType());
var constructor = subtypeofTBase.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, types, null);
if (constructor != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@ public void Dispose()
// that should never happen but Dispose in general is expected to be safe to call so... let's obey the rules
return;
}
var reversed = localCache.Values.Reverse();
foreach (var burden in reversed)
{
burden.Release();
}
localCache.Values.Reverse().ForEach(b => b.Release());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public virtual void ProcessModel(IKernel kernel, ComponentModel model)
protected virtual ConstructorCandidate CreateConstructorCandidate(ComponentModel model, ConstructorInfo constructor)
{
var parameters = constructor.GetParameters();
var dependencies = parameters.Select(e => BuildParameterDependency(e)).ToArray();
var dependencies = parameters.ConvertAll(BuildParameterDependency);
return new ConstructorCandidate(constructor, dependencies);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ public InterceptorGroup<TService> Interceptors(params InterceptorReference[] int
/// <returns> </returns>
public ComponentRegistration<TService> Interceptors(params Type[] interceptors)
{
var references = interceptors.Select(t => new InterceptorReference(t)).ToArray();
var references = interceptors.ConvertAll(t => new InterceptorReference(t));
return AddDescriptor(new InterceptorDescriptor(references));
}

Expand Down Expand Up @@ -633,7 +633,7 @@ public ComponentRegistration<TService> Interceptors<TInterceptor1, TInterceptor2
/// <returns> </returns>
public ComponentRegistration<TService> Interceptors(params string[] keys)
{
var interceptors = keys.Select(e => InterceptorReference.ForKey(e)).ToArray();
var interceptors = keys.ConvertAll(InterceptorReference.ForKey);
return AddDescriptor(new InterceptorDescriptor(interceptors));
}

Expand Down Expand Up @@ -813,7 +813,7 @@ public ComponentRegistration<TService> OnCreate(params Action<TService>[] action
{
return this;
}
return OnCreate(actions.Select(a => new LifecycleActionDelegate<TService>((_, o) => a(o))).ToArray());
return OnCreate(actions.ConvertAll(a => new LifecycleActionDelegate<TService>((_, o) => a(o))));
}

/// <summary>
Expand Down Expand Up @@ -848,7 +848,7 @@ public ComponentRegistration<TService> OnDestroy(params Action<TService>[] actio
{
return this;
}
return OnDestroy(actions.Select(a => new LifecycleActionDelegate<TService>((_, o) => a(o))).ToArray());
return OnDestroy(actions.ConvertAll(a => new LifecycleActionDelegate<TService>((_, o) => a(o))));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public partial class DefaultDiagnosticsSubSystem :

public override void Terminate()
{
foreach(var element in diagnostics.Values.OfType<IDisposable>())
{
element.Dispose();
}
diagnostics.Values.OfType<IDisposable>().ForEach(e => e.Dispose());
}

public void AddDiagnostic<TDiagnostic>(TDiagnostic diagnostic) where TDiagnostic : IDiagnostic<object>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,8 @@ protected virtual void SetUpComponents(IConfiguration[] configurations, IWindsor
AssertImplementsService(component, firstService, implementation);
var defaults = CastleComponentAttribute.GetDefaultsFor(implementation);
if (defaults.ServicesSpecifiedExplicitly && services.Count == 0)
{
foreach(var service in defaults.Services)
{
services.Add(service);
}
{
defaults.Services.ForEach(s => services.Add(s));
}
name = GetName(defaults, component);
}
Expand Down

0 comments on commit c52f999

Please sign in to comment.