/src/LinFu.AOP.Interfaces/SimpleMethodReplacementProvider.cs

http://github.com/philiplaureano/LinFu · C# · 60 lines · 23 code · 6 blank · 31 comment · 2 complexity · 4585f477ba438ebe3faf75168b9f308f MD5 · raw file

  1. using System;
  2. namespace LinFu.AOP.Interfaces
  3. {
  4. /// <summary>
  5. /// Represents the simplest possible <see cref="IMethodReplacementProvider" /> implementation
  6. /// that will allow the user to use the original method body implementation as part
  7. /// of the interceptor call.
  8. /// </summary>
  9. public class SimpleMethodReplacementProvider : BaseMethodReplacementProvider
  10. {
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="SimpleMethodReplacementProvider" /> class.
  13. /// </summary>
  14. /// <param name="replacement">The method body replacement interceptor.</param>
  15. public SimpleMethodReplacementProvider(IInterceptor replacement)
  16. {
  17. MethodReplacement = replacement;
  18. }
  19. /// <summary>
  20. /// Gets or sets the value indicating the Predicate that will determine whether or not
  21. /// the method should be intercepted.
  22. /// </summary>
  23. /// <value>The interceptor predicate.</value>
  24. public Func<IInvocationInfo, bool> MethodReplacementPredicate { get; set; }
  25. /// <summary>
  26. /// Gets or sets the value indicating the actual <see cref="IInterceptor" />
  27. /// instance that will provide the method body implementations.
  28. /// </summary>
  29. /// <value>The interceptor that will swap the method bodies at runtime.</value>
  30. public IInterceptor MethodReplacement { get; set; }
  31. /// <summary>
  32. /// Determines whether or not a particular method body should be replaced at runtime.
  33. /// </summary>
  34. /// <param name="host">The host instance that contains the target method.</param>
  35. /// <param name="context">The context surrounding the method call.</param>
  36. /// <returns>Returns <c>true</c> if the method body should be swapped; otherwise, it will return <c>false</c>.</returns>
  37. protected override bool ShouldReplace(object host, IInvocationInfo context)
  38. {
  39. if (MethodReplacementPredicate == null)
  40. return true;
  41. return MethodReplacementPredicate(context);
  42. }
  43. /// <summary>
  44. /// Gets the method replacement for a given <see cref="IInvocationInfo">invocation context</see>.
  45. /// </summary>
  46. /// <param name="host">The host instance that contains the target method.</param>
  47. /// <param name="context">The context surrounding the method call.</param>
  48. /// <returns>The interceptor that will swap the method bodies at runtime.</returns>
  49. protected override IInterceptor GetReplacement(object host, IInvocationInfo context)
  50. {
  51. return MethodReplacement;
  52. }
  53. }
  54. }