/src/LinFu.AOP/Extensions/TypeDefinitionExtensions.cs

http://github.com/philiplaureano/LinFu · C# · 129 lines · 72 code · 12 blank · 45 comment · 9 complexity · 472325ebe5d53883e821fcd12538dc54 MD5 · raw file

  1. using System;
  2. using System.Linq;
  3. using LinFu.AOP.Cecil.Interfaces;
  4. using Mono.Cecil;
  5. namespace LinFu.AOP.Cecil.Extensions
  6. {
  7. /// <summary>
  8. /// Adds helper methods to the <see cref="TypeDefinition" /> class.
  9. /// </summary>
  10. public static class TypeDefinitionExtensions
  11. {
  12. /// <summary>
  13. /// Applies a <see cref="IMethodWeaver" /> instance to all methods
  14. /// within the given <paramref name="targetType" />.
  15. /// </summary>
  16. /// <param name="targetType">The target module.</param>
  17. /// <param name="weaver">The <see cref="ITypeWeaver" /> instance that will modify the methods in the given target type.</param>
  18. public static void WeaveWith(this TypeDefinition targetType, IMethodWeaver weaver)
  19. {
  20. var module = targetType.Module;
  21. var targetMethods = from MethodDefinition method in targetType.Methods
  22. where weaver.ShouldWeave(method)
  23. select method;
  24. // Modify the host module
  25. weaver.ImportReferences(module);
  26. // Add any additional members to the target type
  27. weaver.AddAdditionalMembers(targetType);
  28. foreach (var item in targetMethods)
  29. weaver.Weave(item);
  30. }
  31. /// <summary>
  32. /// Modifies all the methods in an assembly using the given method weaver.
  33. /// </summary>
  34. /// <param name="targetAssembly">The target assembly to be modified.</param>
  35. /// <param name="weaver">The weaver that will perform the modification.</param>
  36. public static void WeaveWith(this AssemblyDefinition targetAssembly, IMethodWeaver weaver)
  37. {
  38. Func<MethodReference, bool> defaultFilter = m => m.IsDefinition;
  39. WeaveWith(targetAssembly, weaver, defaultFilter);
  40. }
  41. /// <summary>
  42. /// Modifies all the types in an assembly using the given type weaver.
  43. /// </summary>
  44. /// <param name="targetAssembly">The target assembly to be modified.</param>
  45. /// <param name="weaver">The weaver that will perform the modification.</param>
  46. public static void WeaveWith(this AssemblyDefinition targetAssembly, ITypeWeaver weaver)
  47. {
  48. WeaveWith(targetAssembly, weaver, _ => true);
  49. }
  50. /// <summary>
  51. /// Modifies the chosen types in an assembly using the given type weaver.
  52. /// </summary>
  53. /// <param name="targetAssembly">The target assembly to be modified</param>
  54. /// <param name="weaver">The type weaver that will be used to modify the selected types.</param>
  55. /// <param name="typeFilter">The filter that will determine which types should be modified.</param>
  56. public static void WeaveWith(this AssemblyDefinition targetAssembly, ITypeWeaver weaver,
  57. Func<TypeReference, bool> typeFilter)
  58. {
  59. var module = targetAssembly.MainModule;
  60. var types = module.Types.Where(t => typeFilter(t) && t.IsDefinition && weaver.ShouldWeave(t)).ToArray();
  61. foreach (var type in types)
  62. {
  63. weaver.Weave(type);
  64. }
  65. }
  66. /// <summary>
  67. /// Modifies the chosen types in an assembly using the given method weaver.
  68. /// </summary>
  69. /// <param name="targetAssembly">The target assembly that will be modified.</param>
  70. /// <param name="weaver">The method weaver that will be used to rewrite all the target methods.</param>
  71. /// <param name="typeFilter">The predicate that will determine which types will be modified.</param>
  72. public static void WeaveWith(this AssemblyDefinition targetAssembly, IMethodWeaver weaver,
  73. Func<TypeReference, bool> typeFilter)
  74. {
  75. var module = targetAssembly.MainModule;
  76. var types = module.Types.Where(t => typeFilter(t) && t.IsDefinition).ToArray();
  77. var methods = types.SelectMany(t => t.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m))).ToArray();
  78. foreach (var method in methods)
  79. {
  80. weaver.Weave(method);
  81. }
  82. }
  83. /// <summary>
  84. /// Modifies the chosen types in an assembly using the given method weaver.
  85. /// </summary>
  86. /// <param name="targetAssembly">The target assembly that will be modified.</param>
  87. /// <param name="weaver">The method weaver that will be used to rewrite all the target methods.</param>
  88. /// <param name="methodFilter">The predicate that will determine which methods will be modified.</param>
  89. public static void WeaveWith(this AssemblyDefinition targetAssembly, IMethodWeaver weaver,
  90. Func<MethodReference, bool> methodFilter)
  91. {
  92. var module = targetAssembly.MainModule;
  93. var types = module.Types.Where(t => !t.IsInterface && !t.IsValueType).ToArray();
  94. var methods = types.SelectMany(t => t.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m) && methodFilter(m))).ToArray();
  95. foreach (var method in methods)
  96. {
  97. weaver.Weave(method);
  98. }
  99. }
  100. /// <summary>
  101. /// Modifies the current type using the given method weaver.
  102. /// </summary>
  103. /// <param name="targetType">The target type to be modified.</param>
  104. /// <param name="weaver">The method weaver that will modify the selected methods.</param>
  105. /// <param name="methodFilter">The method filter that will determine which methods should be modified.</param>
  106. public static void WeaveWith(this TypeDefinition targetType, IMethodWeaver weaver,
  107. Func<MethodReference, bool> methodFilter)
  108. {
  109. var methods = targetType.Methods.Where(m => m.HasBody && weaver.ShouldWeave(m) && methodFilter(m))
  110. .ToArray();
  111. foreach (var method in methods)
  112. {
  113. weaver.Weave(method);
  114. }
  115. }
  116. }
  117. }