// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // The image processor bootstrapper. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Configuration { using System; using System.Collections.Generic; using System.Linq; using ImageProcessor.Core.Common.Exceptions; using ImageProcessor.Imaging.Formats; /// /// The image processor bootstrapper. /// public class ImageProcessorBootstrapper { /// /// A new instance Initializes a new instance of the class. /// with lazy initialization. /// private static readonly Lazy Lazy = new Lazy(() => new ImageProcessorBootstrapper()); /// /// Prevents a default instance of the class from being created. /// private ImageProcessorBootstrapper() { this.LoadSupportedImageFormats(); } /// /// Gets the current instance of the class. /// public static ImageProcessorBootstrapper Instance { get { return Lazy.Value; } } /// /// Gets the supported image formats. /// public IEnumerable SupportedImageFormats { get; private set; } /// /// Creates a list, using reflection, of supported image formats that ImageProcessor can run. /// private void LoadSupportedImageFormats() { if (this.SupportedImageFormats == null) { try { Type type = typeof(ISupportedImageFormat); List availableTypes = AppDomain.CurrentDomain .GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(t => t != null && type.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract) .ToList(); this.SupportedImageFormats = availableTypes .Select(x => (Activator.CreateInstance(x) as ISupportedImageFormat)).ToList(); } catch (Exception ex) { throw new ImageFormatException(ex.Message, ex.InnerException); } } } } }