PageRenderTime 18ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.Reflection/AssemblyTargetLoader.cs

http://github.com/philiplaureano/LinFu
C# | 123 lines | 57 code | 13 blank | 53 comment | 5 complexity | 4027e3066786fd2e526afee8a25b8b47 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using System.Reflection;
  5
  6namespace LinFu.Reflection
  7{
  8    /// <summary>
  9    ///     Represents a loader class that takes <see cref="System.Type" />
 10    ///     instances as input and generates <see cref="Action{T}" />
 11    ///     instances that can be used to configure a <typeparamref name="TTarget" />
 12    ///     instance.
 13    /// </summary>
 14    /// <typeparam name="TTarget">The target type to configure.</typeparam>
 15    /// <typeparam name="TAssembly">The assembly type.</typeparam>
 16    /// <typeparam name="TType">The target type.</typeparam>
 17    public class AssemblyTargetLoader<TTarget, TAssembly, TType> : IAssemblyTargetLoader<TTarget, TAssembly, TType>,
 18        IActionLoader<TTarget, string>
 19    {
 20        private IActionLoader<IList<Action<TTarget>>, TAssembly> _assemblyActionLoader;
 21        private IAssemblyLoader<TAssembly> _assemblyLoader;
 22
 23        /// <summary>
 24        ///     Initializes a new instance of the <see cref="AssemblyTargetLoader{TTarget,TAssembly,TType}" /> class.
 25        /// </summary>
 26        public AssemblyTargetLoader(ITypeExtractor<TAssembly, TType> typeExtractor,
 27            IAssemblyLoader<TAssembly> assemblyLoader)
 28        {
 29            _assemblyActionLoader =
 30                new AssemblyActionLoader<TTarget, TAssembly, TType>(() => TypeLoaders, typeExtractor);
 31            _assemblyLoader = assemblyLoader;
 32        }
 33
 34        /// <summary>
 35        ///     Gets or sets the value indicating the action loader
 36        ///     responsible for reading an assembly and converts it to
 37        ///     a list of actions to be performed against the target type.
 38        /// </summary>
 39        public virtual IActionLoader<IList<Action<TTarget>>, TAssembly> AssemblyActionLoader
 40        {
 41            get => _assemblyActionLoader;
 42            set => _assemblyActionLoader = value;
 43        }
 44
 45
 46        /// <summary>
 47        ///     The <see cref="IAssemblyLoader" /> instance that will load
 48        ///     the target assemblies.
 49        /// </summary>
 50        public virtual IAssemblyLoader<TAssembly> AssemblyLoader
 51        {
 52            get => _assemblyLoader;
 53            set => _assemblyLoader = value;
 54        }
 55
 56        /// <summary>
 57        ///     The list of ActionLoaders that will be used to
 58        ///     configure the target.
 59        /// </summary>
 60        public virtual IList<IActionLoader<TTarget, TType>> TypeLoaders { get; } =
 61            new List<IActionLoader<TTarget, TType>>();
 62
 63        /// <summary>
 64        ///     Determines whether or not the current type loader
 65        ///     instance can load the current file type.
 66        /// </summary>
 67        /// <remarks>
 68        ///     This class only loads assemblies with the ".dll" extension.
 69        /// </remarks>
 70        /// <param name="filename">The filename and full path of the target file.</param>
 71        /// <returns>Returns <c>true</c> if the file can be loaded; otherwise, the result is <c>false</c>.</returns>
 72        public virtual bool CanLoad(string filename)
 73        {
 74            var extension = Path.GetExtension(filename).ToLower();
 75            return TypeLoaders.Count > 0 &&
 76                   (extension == ".dll" || extension == ".exe") &&
 77                   File.Exists(filename);
 78        }
 79
 80        /// <summary>
 81        ///     Reads an input file using the given <paramref name="filename" />
 82        ///     and converts it into a set of <see cref="Action{TTarget}" />
 83        ///     instances that can be applied to a target class instance..
 84        /// </summary>
 85        /// <remarks>This class can only load valid .NET Assemblies.</remarks>
 86        /// <param name="filename">The target file to be loaded.</param>
 87        /// <returns>A set of <see cref="Action{IServiceContainer}" /> instances to apply to a target type.</returns>
 88        public virtual IEnumerable<Action<TTarget>> Load(string filename)
 89        {
 90            var assembly = default(TAssembly);
 91
 92            if (AssemblyLoader == null)
 93                throw new ArgumentException("The assembly loader cannot be null");
 94
 95            // Load the assembly into memory
 96            assembly = AssemblyLoader.Load(filename);
 97
 98            var results = new List<Action<TTarget>>();
 99            var listActions = AssemblyActionLoader.Load(assembly);
100            foreach (var action in listActions) action(results);
101
102            return results;
103        }
104    }
105
106    /// <summary>
107    ///     Represents a loader class that takes <see cref="System.Type" />
108    ///     instances as input and generates <see cref="Action{T}" />
109    ///     instances that can be used to configure a <typeparamref name="TTarget" />
110    ///     instance.
111    /// </summary>
112    /// <typeparam name="TTarget">The target type to configure.</typeparam>
113    public class AssemblyTargetLoader<TTarget> : AssemblyTargetLoader<TTarget, Assembly, Type>
114    {
115        /// <summary>
116        ///     Initializes the class with the default property values.
117        /// </summary>
118        public AssemblyTargetLoader()
119            : base(new TypeExtractor(), new AssemblyLoader())
120        {
121        }
122    }
123}