/src/LinFu.IoC/Interceptors/BaseInterceptor.cs

http://github.com/philiplaureano/LinFu · C# · 66 lines · 27 code · 8 blank · 31 comment · 0 complexity · 78938f4e34cff56500a96d639cf278cb MD5 · raw file

  1. using System.Reflection;
  2. using LinFu.AOP.Interfaces;
  3. using LinFu.IoC.Configuration;
  4. using LinFu.IoC.Configuration.Interfaces;
  5. namespace LinFu.IoC.Interceptors
  6. {
  7. /// <summary>
  8. /// A class that provides the most basic functionality for an interceptor.
  9. /// </summary>
  10. public abstract class BaseInterceptor : IInterceptor
  11. {
  12. /// <summary>
  13. /// The default constructor.
  14. /// </summary>
  15. protected BaseInterceptor()
  16. {
  17. MethodInvoker = new MethodInvoke();
  18. }
  19. /// <summary>
  20. /// Initializes the class with the <paramref name="methodInvoke" /> instance.
  21. /// </summary>
  22. /// <param name="methodInvoke">The <see cref="IMethodInvoke{TMethod}" /> instance that will invoke the target method.</param>
  23. protected BaseInterceptor(IMethodInvoke<MethodInfo> methodInvoke)
  24. {
  25. MethodInvoker = methodInvoke;
  26. }
  27. /// <summary>
  28. /// Gets the value indicating the <see cref="IMethodInvoke{TMethod}" /> instance
  29. /// that will be used to invoke the target method.
  30. /// </summary>
  31. protected IMethodInvoke<MethodInfo> MethodInvoker { get; }
  32. /// <summary>
  33. /// Intercepts a method call using the given
  34. /// <see cref="IInvocationInfo" /> instance.
  35. /// </summary>
  36. /// <param name="info">
  37. /// The <see cref="IInvocationInfo" /> instance that will
  38. /// contain all the necessary information associated with a
  39. /// particular method call.
  40. /// </param>
  41. /// <returns>
  42. /// The return value of the target method. If the return type of the target
  43. /// method is <see cref="void" />, then the return value will be ignored.
  44. /// </returns>
  45. public virtual object Intercept(IInvocationInfo info)
  46. {
  47. var target = GetTarget(info);
  48. var method = info.TargetMethod;
  49. var arguments = info.Arguments;
  50. return MethodInvoker.Invoke(target, (MethodInfo) method, arguments);
  51. }
  52. /// <summary>
  53. /// Gets the target object instance.
  54. /// </summary>
  55. /// <param name="info">The <see cref="IInvocationInfo" /> instance that describes the current execution context.</param>
  56. protected abstract object GetTarget(IInvocationInfo info);
  57. }
  58. }