/src/LinFu.AOP/Extensions/MethodBodyInterceptionExtensions.cs

http://github.com/philiplaureano/LinFu · C# · 94 lines · 53 code · 10 blank · 31 comment · 2 complexity · 6489319cdc1dfceb204c192b1086763c MD5 · raw file

  1. using System;
  2. using LinFu.AOP.Cecil.Interfaces;
  3. using LinFu.AOP.Interfaces;
  4. using Mono.Cecil;
  5. namespace LinFu.AOP.Cecil.Extensions
  6. {
  7. /// <summary>
  8. /// Represents an extension class that adds method body interception support to the Mono.Cecil object model.
  9. /// </summary>
  10. public static class MethodBodyInterceptionExtensions
  11. {
  12. /// <summary>
  13. /// Intercepts all method bodies on the target item.
  14. /// </summary>
  15. /// <param name="target">The target to be modified.</param>
  16. public static void InterceptAllMethodBodies(this TypeDefinition target)
  17. {
  18. target.InterceptMethodBody(m => true);
  19. }
  20. /// <summary>
  21. /// Intercepts all method bodies on the target item.
  22. /// </summary>
  23. /// <param name="target">The target to be modified.</param>
  24. public static void InterceptAllMethodBodies(this AssemblyDefinition target)
  25. {
  26. target.InterceptMethodBody(m => true);
  27. }
  28. /// <summary>
  29. /// Intercepts all method bodies on the target assembly.
  30. /// </summary>
  31. /// <param name="target">The target assembly that will be modified.</param>
  32. /// <param name="methodFilter">The method filter that will be used to determine which methods will be modified.</param>
  33. public static void InterceptMethodBody(this AssemblyDefinition target,
  34. IMethodFilter methodFilter)
  35. {
  36. target.InterceptMethodBody(methodFilter.ShouldWeave);
  37. }
  38. /// <summary>
  39. /// Intercepts all method bodies on the target item.
  40. /// </summary>
  41. /// <param name="target">The target to be modified.</param>
  42. /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
  43. public static void InterceptMethodBody(this TypeDefinition target, IMethodFilter methodFilter)
  44. {
  45. target.InterceptMethodBody(methodFilter.ShouldWeave);
  46. }
  47. /// <summary>
  48. /// Intercepts all method bodies on the target item.
  49. /// </summary>
  50. /// <param name="target">The target to be modified.</param>
  51. /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
  52. public static void InterceptMethodBody(this AssemblyDefinition target,
  53. Func<MethodReference, bool> methodFilter)
  54. {
  55. var typeFilter = GetTypeFilter();
  56. target.Accept(new ImplementModifiableType(typeFilter));
  57. IMethodWeaver interceptMethodBody = new InterceptMethodBody(methodFilter);
  58. target.WeaveWith(interceptMethodBody);
  59. }
  60. /// <summary>
  61. /// Intercepts all method bodies on the target item.
  62. /// </summary>
  63. /// <param name="target">The target to be modified.</param>
  64. /// <param name="methodFilter">The method filter that will determine the methods that will be modified.</param>
  65. public static void InterceptMethodBody(this TypeDefinition target,
  66. Func<MethodReference, bool> methodFilter)
  67. {
  68. var typeFilter = GetTypeFilter();
  69. target.Accept(new ImplementModifiableType(typeFilter));
  70. IMethodWeaver interceptMethodBody = new InterceptMethodBody(methodFilter);
  71. target.WeaveWith(interceptMethodBody);
  72. }
  73. private static Func<TypeReference, bool> GetTypeFilter()
  74. {
  75. return type =>
  76. {
  77. var actualType = type.Resolve();
  78. if (actualType.IsValueType || actualType.IsInterface)
  79. return false;
  80. return actualType.IsClass;
  81. };
  82. }
  83. }
  84. }