/src/LinFu.AOP/NewInstanceInterceptionAdapter.cs

http://github.com/philiplaureano/LinFu · C# · 38 lines · 19 code · 4 blank · 15 comment · 0 complexity · 3bfbd143e60ac4d4027f10948ccd6b41 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 an adapter class that maps <see cref="INewInstanceFilter" /> instances to
  8. /// functors.
  9. /// </summary>
  10. public class NewInstanceInterceptionAdapter : INewInstanceFilter
  11. {
  12. private readonly Func<MethodReference, TypeReference, MethodReference, bool> _filter;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="NewInstanceInterceptionAdapter" /> class.
  15. /// </summary>
  16. /// <param name="filter">The filter that determines which instances will be intercepted.</param>
  17. public NewInstanceInterceptionAdapter(Func<MethodReference, TypeReference, MethodReference, bool> filter)
  18. {
  19. _filter = filter;
  20. }
  21. /// <summary>
  22. /// Determines whether or not a particular constructor call should be intercepted by the postweaver.
  23. /// </summary>
  24. /// <param name="currentConstructor">The constructor used to instantiate the current instance.</param>
  25. /// <param name="concreteType">The concrete type that contains the new instance call.</param>
  26. /// <param name="hostMethod">The host method that contains the new operator call.</param>
  27. /// <returns>Returns <c>true</c> if the new operator call should be intercepted; otherwise, it should return <c>false</c>.</returns>
  28. public bool ShouldWeave(MethodReference currentConstructor, TypeReference concreteType,
  29. MethodReference hostMethod)
  30. {
  31. return _filter(currentConstructor, concreteType, hostMethod);
  32. }
  33. }
  34. }