/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

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