PageRenderTime 37ms CodeModel.GetById 29ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LinFu.Reflection/AssemblyActionLoader.cs

http://github.com/philiplaureano/LinFu
C# | 133 lines | 58 code | 14 blank | 61 comment | 12 complexity | 497215cdc4f1399657a30bd4f2b8bb4f MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Reflection;
  5
  6namespace LinFu.Reflection
  7{
  8    /// <summary>
  9    ///     A class that reads an assembly and converts it into a set of actions
 10    ///     that can be used to build the list of actions against the <typeparamref name="TTarget" />
 11    ///     type.
 12    /// </summary>
 13    /// <typeparam name="TTarget">The target type.</typeparam>
 14    /// <typeparam name="TAssembly">The assembly type.</typeparam>
 15    /// <typeparam name="TType">The target input type.</typeparam>
 16    public class AssemblyActionLoader<TTarget, TAssembly, TType> : IActionLoader<IList<Action<TTarget>>, TAssembly>
 17    {
 18        private readonly Func<IList<IActionLoader<TTarget, TType>>> _getTypeLoaders;
 19
 20        /// <summary>
 21        ///     Initializes the class with a set of <see cref="IActionLoader{TTarget,Type}" />
 22        ///     instances that will be used to load the target assembly.
 23        /// </summary>
 24        /// <param name="getTypeLoaders">The delegate that will return the actual list of typeloaders.</param>
 25        /// <param name="typeExtractor">
 26        ///     The type extractor that will be responsible for pulling the types out of the current
 27        ///     assembly.
 28        /// </param>
 29        public AssemblyActionLoader(Func<IList<IActionLoader<TTarget, TType>>> getTypeLoaders,
 30            ITypeExtractor<TAssembly, TType> typeExtractor)
 31        {
 32            _getTypeLoaders = getTypeLoaders;
 33            TypeExtractor = typeExtractor;
 34        }
 35
 36        /// <summary>
 37        ///     The <see cref="ITypeExtractor" /> instance that will
 38        ///     determine which types will be extracted from an assembly.
 39        /// </summary>
 40        public ITypeExtractor<TAssembly, TType> TypeExtractor { get; set; }
 41
 42
 43        /// <summary>
 44        ///     Loads the target assembly and creates an action that can
 45        ///     create the list of actions which will modify the <typeparamref name="TTarget" /> instance.
 46        /// </summary>
 47        /// <param name="input">The target assembly.</param>
 48        /// <returns>
 49        ///     The list of actions which will modify the list of actions that will be executed against the
 50        ///     <typeparamref name="TTarget" /> instance.
 51        /// </returns>
 52        public IEnumerable<Action<IList<Action<TTarget>>>> Load(TAssembly input)
 53        {
 54            yield return list => CreateActionsFrom(input, list);
 55        }
 56
 57        /// <summary>
 58        ///     Determines if an assembly can be loaded.
 59        /// </summary>
 60        /// <param name="assembly">The target assembly.</param>
 61        /// <returns>Returns <c>true</c> if the assembly is not <c>null</c>.</returns>
 62        public virtual bool CanLoad(TAssembly assembly)
 63        {
 64            return assembly != null;
 65        }
 66
 67
 68        /// <summary>
 69        ///     Generates a list of actions from a target assemby.
 70        /// </summary>
 71        /// <param name="assembly">The target assembly.</param>
 72        /// <param name="resultList">The list that will store the resulting actions.</param>
 73        private void CreateActionsFrom(TAssembly assembly, ICollection<Action<TTarget>> resultList)
 74        {
 75            // Grab the types embedded in the assembly
 76            IEnumerable<TType> types = new TType[0];
 77            if (assembly != null && TypeExtractor != null)
 78                types = TypeExtractor.GetTypes(assembly);
 79
 80            // Pass the loaded types to
 81            // the type loaders for processing
 82            foreach (var type in types)
 83            {
 84                // Skip any invalid types
 85                if (type == null)
 86                    continue;
 87
 88                LoadResultsFromType(type, resultList);
 89            }
 90        }
 91
 92        /// <summary>
 93        ///     Generates the list of <see cref="Action{TTarget}" />
 94        ///     instances which will be used to configure a target instance.
 95        /// </summary>
 96        /// <param name="type">The <see cref="Type" /> instance that holds the configuration information.</param>
 97        /// <param name="results">The list that will hold the actions which will configure the container.</param>
 98        private void LoadResultsFromType(TType type, ICollection<Action<TTarget>> results)
 99        {
100            var typeLoaders = _getTypeLoaders();
101            foreach (var typeLoader in typeLoaders)
102            {
103                if (typeLoader == null || !typeLoader.CanLoad(type))
104                    continue;
105
106                var actions = typeLoader.Load(type);
107                if (actions.Count() == 0)
108                    continue;
109
110                foreach (var action in actions) results.Add(action);
111            }
112        }
113    }
114
115    /// <summary>
116    ///     A class that reads an assembly and converts it into a set of actions
117    ///     that can be used to build the list of actions against the <typeparamref name="TTarget" />
118    ///     type.
119    /// </summary>
120    /// <typeparam name="TTarget">The target type.</typeparam>
121    public class AssemblyActionLoader<TTarget> : AssemblyActionLoader<TTarget, Assembly, Type>
122    {
123        /// <summary>
124        ///     Initializes the class with a set of <see cref="IActionLoader{TTarget,Type}" />
125        ///     instances that will be used to load the target assembly.
126        /// </summary>
127        /// <param name="getTypeLoaders">The delegate that will return the actual list of typeloaders.</param>
128        public AssemblyActionLoader(Func<IList<IActionLoader<TTarget, Type>>> getTypeLoaders)
129            : base(getTypeLoaders, new TypeExtractor())
130        {
131        }
132    }
133}