using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Logging;
namespace Umbraco.Core
{
internal static class ServiceProviderExtensions
{
///
/// Used to create instances of the specified type based on the resolved/cached plugin types
///
///
///
///
///
/// set to true if an exception is to be thrown if there is an error during instantiation
///
public static IEnumerable CreateInstances(this IServiceProvider serviceProvider, IEnumerable types, ILogger logger, bool throwException = false)
{
var typesAsArray = types.ToArray();
var instances = new List();
foreach (var t in typesAsArray)
{
try
{
var typeInstance = (T) serviceProvider.GetService(t);
instances.Add(typeInstance);
}
catch (Exception ex)
{
logger.Error(String.Format("Error creating type {0}", t.FullName), ex);
if (throwException)
{
throw;
}
}
}
return instances;
}
}
}