/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

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