/// <summary>
/// Fügt Dienste des angegebenen Assembly hinzu.
/// </summary>
/// <param name = "assembly">Das Assembly, das nach Services durchsucht werden soll.</param>
public void AddServices(Assembly assembly)
{
// Beschreibungen der Klassen mit ServiceAttribute
var services = from type in assembly.GetTypes()
let attributes = type.GetCustomAttributes(typeof (ServiceAttribute), false)
where attributes.Length > 0 && !type.IsAbstract
select new {type, attributes};
var initializableServices = new List<Type>();
// Dienste hinzufügen
foreach (var service in services)
{
foreach (ServiceAttribute attribute in service.attributes)
{
var serviceInterface = attribute.ServiceInterface;
if (serviceInterface == null)
{
if (service.attributes.Length != 1)
{
throw new InvalidOperationException(
"There must be only one service attribute when no interface is specified.");
}
var implementedInterface = service.type.GetInterfaces()
.SingleOrDefault(
i => i.GetCustomAttributes(typeof (ServiceInterfaceAttribute), false).Length > 0);
if (implementedInterface == null)
throw new InvalidOperationException(Resources.Ex_ServiceAttributeNotExplicit);
serviceInterface = implementedInterface;
}
ServiceLocator.Current.RegisterService(serviceInterface, service.type, false);
var isInitializable = service.type.GetInterfaces()
.Any(i => i.IsAssignableFrom(typeof(IInitializeAsync)));
if (isInitializable)
initializableServices.Add(serviceInterface);
}
}
InitServices(initializableServices);
}