PageRenderTime 158ms CodeModel.GetById 146ms RepoModel.GetById 0ms app.codeStats 0ms

/src/LinFu.IoC/Configuration/Loaders/PreprocessorLoader.cs

http://github.com/philiplaureano/LinFu
C# | 76 lines | 44 code | 8 blank | 24 comment | 6 complexity | 3da002b0d35c8163b5be8959d9ec15b9 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using LinFu.IoC.Interfaces;
  6. using LinFu.Reflection;
  7. namespace LinFu.IoC.Configuration.Loaders
  8. {
  9. /// <summary>
  10. /// A class that automatically loads <see cref="IPreProcessor" />
  11. /// instances and configures a loader to inject those postprocessors
  12. /// into a container upon initialization.
  13. /// </summary>
  14. internal class PreProcessorLoader : IActionLoader<IServiceContainer, Type>
  15. {
  16. /// <summary>
  17. /// Determines if the plugin loader can load the <paramref name="inputType" />.
  18. /// </summary>
  19. /// <remarks>The target type must implement the <see cref="IPreProcessor" /> interface before it can be loaded into memory.</remarks>
  20. /// <param name="inputType">The target type that might contain the target instance.</param>
  21. /// <returns><c>true</c> if the type can be loaded; otherwise, it returns <c>false</c>.</returns>
  22. public bool CanLoad(Type inputType)
  23. {
  24. try
  25. {
  26. // The type must have a default constructor
  27. var defaultConstructor = inputType.GetConstructor(new Type[0]);
  28. if (defaultConstructor == null)
  29. return false;
  30. // It must have the PreprocessorAttribute defined
  31. var attributes = inputType.GetCustomAttributes(typeof(PreprocessorAttribute), true);
  32. var attributeList = attributes.Cast<PreprocessorAttribute>();
  33. if (attributeList.Count() == 0)
  34. return false;
  35. return typeof(IPreProcessor).IsAssignableFrom(inputType);
  36. }
  37. catch (TypeInitializationException)
  38. {
  39. // Ignore the error
  40. return false;
  41. }
  42. catch (FileNotFoundException)
  43. {
  44. // Ignore the error
  45. return false;
  46. }
  47. }
  48. /// <summary>
  49. /// Loads a set of <see cref="IPreProcessor" /> instances
  50. /// so that they can be loaded into a container upon initialization.
  51. /// </summary>
  52. /// <param name="inputType">The type that will be used to configure the target loader.</param>
  53. /// <returns>A set of <see cref="Action{TTarget}" /> instances. This cannot be <c>null</c>.</returns>
  54. public IEnumerable<Action<IServiceContainer>> Load(Type inputType)
  55. {
  56. var defaultResult = new Action<IServiceContainer>[0];
  57. // Create the postprocessor instance
  58. var instance = Activator.CreateInstance(inputType) as IPreProcessor;
  59. if (instance == null)
  60. return defaultResult;
  61. // Inject the postprocessor into any service containers
  62. // that will be configured by the current loader instance
  63. Action<IServiceContainer> assignPreprocessor =
  64. container => container.PreProcessors.Add(instance);
  65. return new[] {assignPreprocessor};
  66. }
  67. }
  68. }