PageRenderTime 38ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

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