PageRenderTime 102ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 57ms app.codeStats 0ms

/src/LinFu.Reflection/Plugins/BaseTargetLoaderPlugin.cs

http://github.com/philiplaureano/LinFu
C# | 76 lines | 33 code | 8 blank | 35 comment | 0 complexity | b6e6c0920820ce0807b4fa5840918027 MD5 | raw file
 1using System;
 2using System.Reflection;
 3
 4namespace LinFu.Reflection.Plugins
 5{
 6    /// <summary>
 7    ///     A plugin class that provides the basic implementation
 8    ///     for plugins that work with <see cref="IAssemblyTargetLoader{TTarget}" /> instances.
 9    /// </summary>
10    /// <typeparam name="TTarget">The target type being configured.</typeparam>
11    /// <typeparam name="TAssembly">The assembly type.</typeparam>
12    /// <typeparam name="TType">The input type.</typeparam>
13    public abstract class BaseTargetLoaderPlugin<TTarget, TAssembly, TType> : BaseLoaderPlugin<TTarget>,
14        IInitialize<ILoader<TTarget>>
15    {
16        private readonly IAssemblyTargetLoader<TTarget, TAssembly, TType> _assemblyLoader;
17        private readonly ITypeExtractor<TAssembly, TType> _typeExtractor;
18
19        /// <summary>
20        ///     Initializes a new instance of the <see cref="BaseTargetLoaderPlugin{TTarget,TAssembly,TType}" /> class.
21        /// </summary>
22        /// <param name="typeExtractor">The type extractor that will pull the types out of the current assembly.</param>
23        /// <param name="assemblyLoader">The assembly loader that will load the current assembly into memory.</param>
24        protected BaseTargetLoaderPlugin(ITypeExtractor<TAssembly, TType> typeExtractor,
25            IAssemblyTargetLoader<TTarget, TAssembly, TType> assemblyLoader)
26        {
27            _typeExtractor = typeExtractor;
28            _assemblyLoader = assemblyLoader;
29        }
30
31
32        /// <summary>
33        ///     Searches the loader for an <see cref="IAssemblyTargetLoader{T}" />
34        ///     instance and uses its derived classes to initialize
35        ///     the assembly target loader.
36        /// </summary>
37        /// <param name="source">The <see cref="ILoader{TTarget}" /> instance that will hold the plugin.</param>
38        public void Initialize(ILoader<TTarget> source)
39        {
40            Initialize(source, _assemblyLoader);
41        }
42
43
44        /// <summary>
45        ///     Initializes the <paramref name="loader" /> instance
46        ///     with the given <paramref name="assemblyLoader" /> instance.
47        /// </summary>
48        /// <param name="loader">The loader being configured.</param>
49        /// <param name="assemblyLoader">The assembly loader that will load the types into the loader itself.</param>
50        protected abstract void Initialize(ILoader<TTarget> loader,
51            IAssemblyTargetLoader<TTarget, TAssembly, TType> assemblyLoader);
52    }
53
54    /// <summary>
55    ///     A plugin class that provides the basic implementation
56    ///     for plugins that work with <see cref="IAssemblyTargetLoader{TTarget}" /> instances.
57    /// </summary>
58    public abstract class BaseTargetLoaderPlugin<TTarget> : BaseTargetLoaderPlugin<TTarget, Assembly, Type>
59    {
60        /// <summary>
61        ///     Initializes a new instance of the <see cref="BaseTargetLoaderPlugin{TTarget,TAssembly,TType}" /> class.
62        /// </summary>
63        protected BaseTargetLoaderPlugin() : this(new TypeExtractor())
64        {
65        }
66
67        /// <summary>
68        ///     Initializes a new instance of the <see cref="BaseTargetLoaderPlugin{TTarget,TAssembly,TType}" /> class.
69        /// </summary>
70        /// <param name="typeExtractor">The type extractor that will pull the types out of the current assembly.</param>
71        protected BaseTargetLoaderPlugin(ITypeExtractor<Assembly, Type> typeExtractor) :
72            base(typeExtractor, new AssemblyTargetLoader<TTarget, Assembly, Type>(typeExtractor, new AssemblyLoader()))
73        {
74        }
75    }
76}