/src/LinFu.AOP/MethodCallFilterAdapter.cs

http://github.com/philiplaureano/LinFu · C# · 43 lines · 21 code · 4 blank · 18 comment · 1 complexity · 5b442397f0165f32bf2c32e34bd1b7cb MD5 · raw file

  1. using System;
  2. using LinFu.AOP.Cecil.Interfaces;
  3. using Mono.Cecil;
  4. namespace LinFu.AOP.Cecil
  5. {
  6. /// <summary>
  7. /// Represents a type that converts functors into method call filter instances.
  8. /// </summary>
  9. public class MethodCallFilterAdapter : IMethodCallFilter
  10. {
  11. private readonly Func<MethodReference, bool> _hostMethodFilter;
  12. private readonly Func<MethodReference, bool> _methodCallFilter;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="MethodCallFilterAdapter" /> class.
  15. /// </summary>
  16. /// <param name="hostMethodFilter">
  17. /// The method filter that will determine the host methods that will be modified for
  18. /// interception.
  19. /// </param>
  20. /// <param name="methodCallFilter">The method filter that will determine which method calls will be intercepted.</param>
  21. public MethodCallFilterAdapter(Func<MethodReference, bool> hostMethodFilter,
  22. Func<MethodReference, bool> methodCallFilter)
  23. {
  24. _hostMethodFilter = hostMethodFilter;
  25. _methodCallFilter = methodCallFilter;
  26. }
  27. /// <summary>
  28. /// Determines whether or not a particular method call should be intercepted.
  29. /// </summary>
  30. /// <param name="targetType">The host type that contains the method call.</param>
  31. /// <param name="hostMethod">The method that contains the current method call.</param>
  32. /// <param name="currentMethodCall">The method call to be intercepted.</param>
  33. /// <returns>Returns <c>true</c> if the method call should be intercepted; otherwise, it will return <c>false</c>.</returns>
  34. public bool ShouldWeave(TypeReference targetType, MethodReference hostMethod, MethodReference currentMethodCall)
  35. {
  36. return _hostMethodFilter(hostMethod) && _methodCallFilter(currentMethodCall);
  37. }
  38. }
  39. }