/src/LinFu.IoC/Configuration/Injectors/AutoMethodInjector.cs

http://github.com/philiplaureano/LinFu · C# · 42 lines · 19 code · 3 blank · 20 comment · 0 complexity · 6b16e8b88b70396d51a1c79bf07142fb MD5 · raw file

  1. using System.Linq;
  2. using System.Reflection;
  3. using LinFu.IoC.Configuration.Interfaces;
  4. using LinFu.IoC.Interfaces;
  5. namespace LinFu.IoC.Configuration
  6. {
  7. /// <summary>
  8. /// A class that automatically invokes methods using arguments
  9. /// derived from existing instances from within a <see cref="IServiceContainer" />
  10. /// instance.
  11. /// </summary>
  12. public class AutoMethodInjector : AutoMemberInjector<MethodInfo>
  13. {
  14. /// <summary>
  15. /// Injects services from the <paramref name="container" /> into the target <see cref="MethodInfo" /> instance.
  16. /// </summary>
  17. /// <param name="target">The target object.</param>
  18. /// <param name="method">The <see cref="MethodInfo" /> instance that will store the service instance.</param>
  19. /// <param name="resolver">
  20. /// The <see cref="IArgumentResolver" /> that will determine which arguments will be assigned to the
  21. /// target member.
  22. /// </param>
  23. /// <param name="additionalArguments">
  24. /// The additional arguments that were passed to the <see cref="IServiceRequestResult" />
  25. /// during the instantiation process.
  26. /// </param>
  27. /// <param name="container">The container that will provide the service instances.</param>
  28. protected override void Inject(object target, MethodInfo method,
  29. IArgumentResolver resolver, IServiceContainer container,
  30. object[] additionalArguments)
  31. {
  32. var parameterTypes = from p in method.GetParameters()
  33. select new NamedType(p) as INamedType;
  34. var arguments = resolver.ResolveFrom(parameterTypes, container, additionalArguments);
  35. // Invoke the target method
  36. method.Invoke(target, arguments);
  37. }
  38. }
  39. }